在 MongoDB 3.0 中获取插入的文档
Get the inserted document(s) in MongoDB 3.0
似乎 db.collection.insert()
used to accept a callback function as last argument but the api has since changed and now it just returns a writeResult
对象仅报告插入的文档数..
如何获取刚刚插入的实际文档(或至少是它们的 _id)?
更新
是的,我是 运行 我在 mongo
shell 中的脚本,我现在意识到我上面链接的另一个堆栈溢出问题是关于 node.js 驱动程序的,它有一个不同的API(虽然它也是 javascript 并且看起来很相似)所以:
mongo
shell 从未报告过插入文档的详细信息,现在也没有报告(如果我错了请纠正我)
- 下面有一个公认的答案,显示了如何在 Python
中实现这一点
shell 永远不会 returns 这样的“_id”,但是如果您使用任何脚本语言,这就是在 python 中可以完成的方式:
insert_result = db.collection.insert({"_id":10, "key":"value"})
insert_result 将包含最近插入的文档的“_id”,即。 10
似乎 db.collection.insert()
used to accept a callback function as last argument but the api has since changed and now it just returns a writeResult
对象仅报告插入的文档数..
如何获取刚刚插入的实际文档(或至少是它们的 _id)?
更新
是的,我是 运行 我在 mongo
shell 中的脚本,我现在意识到我上面链接的另一个堆栈溢出问题是关于 node.js 驱动程序的,它有一个不同的API(虽然它也是 javascript 并且看起来很相似)所以:
mongo
shell 从未报告过插入文档的详细信息,现在也没有报告(如果我错了请纠正我)- 下面有一个公认的答案,显示了如何在 Python 中实现这一点
shell 永远不会 returns 这样的“_id”,但是如果您使用任何脚本语言,这就是在 python 中可以完成的方式:
insert_result = db.collection.insert({"_id":10, "key":"value"})
insert_result 将包含最近插入的文档的“_id”,即。 10