我正在为 Golang 使用 mgo MongoDB 驱动程序。
插入新对象后能否获取到最后的ObjectId
?
或者我应该用 bson.NewObjectId
手动创建 _id
吗?
最佳答案
对于 mongodb 和 mgo
,您通常需要使用 bson.NewObjectId
自己生成 _id
值。
If the document does not specify an _id field, then MongoDB will add the _id field and assign a unique ObjectId for the document before inserting. Most drivers create an ObjectId and insert the _id field, but the mongod will create and populate the _id if the driver or application does not.
简单地说,自己创建 _id 是可行的方法!
关于go - 如何使用 golang mgo MongoDb 驱动程序获取最后插入的 ObjectId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22497957/