mongodb 插入后如何在PyMongo中获取对象ID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8783753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:24:12  来源:igfitidea点击:

How to get the object id in PyMongo after an insert?

mongodbinsertpymongo

提问by user1125472

I'm doing a simple insert into Mongo...

我正在做一个简单的插入到 Mongo 中......

db.notes.insert({ title: "title", details: "note details"})

After the note document is inserted, I need to get the object id immediately. The result that comes back from the insert has some basic info regarding connection and errors, but no document and field info.

插入笔记文档后,我需要立即获取对象ID。从插入返回的结果有一些关于连接和错误的基本信息,但没有文档和字段信息。

I found some info about using the update() function with upsert=true, I'm just not sure if that's the right way to go, I have not yet tried it.

我找到了一些关于使用 upsert=true 的 update() 函数的信息,我只是不确定这是否是正确的方法,我还没有尝试过。

回答by Tyler Brock

One of the cool things about MongoDB is that the ids are generated client side.

关于 MongoDB 的一件很酷的事情是 ID 是在客户端生成的。

This means you don't even have to ask the server what the id was, because you told it what to save in the first place. Using pymongo the return value of an insert will be the object id. Check it out:

这意味着您甚至不必询问服务器 id 是什么,因为您首先告诉它要保存什么。使用 pymongo,插入的返回值将是对象 ID。一探究竟:

>>> import pymongo
>>> collection = pymongo.Connection()['test']['tyler']
>>> _id = collection.insert({"name": "tyler"})
>>> print _id
4f0b2f55096f7622f6000000

回答by Kumar Saurabh

The answer from Tyler does not work for me. Using _id.inserted_id works

泰勒的回答对我不起作用。使用 _id.inserted_id 有效

>>> import pymongo
>>> collection = pymongo.Connection()['test']['tyler']
>>> _id = collection.insert({"name": "tyler"})
>>> print(_id)
<pymongo.results.InsertOneResult object at 0x0A7EABCD>
>>> print(_id.inserted_id)
5acf02400000000968ba447f

回答by Mehdi Hasirchi

It's better to use insert_one() or insert_many() instead of insert(). Those two are for the newer version. You can use inserted_id to get the id.

最好使用 insert_one() 或 insert_many() 而不是 insert()。这两个是针对较新版本的。您可以使用 insert_id 来获取 id。

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
myDB = myclient["myDB"]
userTable = myDB["Users"]
userDict={"name": "tyler"}

_id = userTable.insert_one(userDict).inserted_id
print(_id)

Or

或者

result = userTable.insert_one(userDict)
print(result.inserted_id)
print(result.acknowledged)

If you need to use insert(), you should write like the lines below

如果你需要使用insert(),你应该像下面这样写

_id = userTable.insert(userDict)
print(_id)

回答by Jurgen Strydom

Newer PyMongo versions depreciate insert, and instead insert_oneor insert_manyshould be used. These functions return a pymongo.results.InsertOneResultor pymongo.results.InsertManyResultobject.

较新的 PyMongo 版本 depreciate insert,而应使用insert_oneor insert_many。这些函数返回一个pymongo.results.InsertOneResultpymongo.results.InsertManyResult对象。

With these objects you can use the .inserted_idand .inserted_idsproperties respectively to get the inserted object ids.

对于这些对象,您可以分别使用.inserted_id.inserted_ids属性来获取插入的对象 ID。

See thislink for more info on insert_oneand insert_manyand thislink for more info on pymongo.results.InsertOneResult.

请参阅链接上的更多信息insert_one,并insert_many链接的更多信息pymongo.results.InsertOneResult

回答by Eve Freeman

updated; removed previous because it wasn't correct

更新; 之前删除了,因为它不正确

It looks like you can also do it with db.notes.save(...), which returns the _id after it performs the insert.

看起来您也可以使用db.notes.save(...),它在执行插入后返回 _id 。

See for more info: http://api.mongodb.org/python/current/api/pymongo/collection.html

有关更多信息,请参阅:http: //api.mongodb.org/python/current/api/pymongo/collection.html

回答by estellezg

You just need to assigne it to some variable:

您只需要将其分配给某个变量:

someVar = db.notes.insert({ title: "title", details: "note details"})