“更新” _id在mongodb文档中
PS:我只需要这样做,因为业务需求,
我能够使用Mongosh来实现它,但是由于要更新有多个记录,我正在尝试实现一个简单的Python脚本来自动化任务。
可以使用pymongodb做到这一点吗?
// store the document in a variable
doc = db.clients.findOne({_id: ObjectId("4cc45467c55f4d2d2a000002")})
// set a new _id on the document
doc._id = ObjectId("4c8a331bda76c559ef000004")
// insert the document, using the new _id
db.clients.insert(doc)
// remove the document with the old _id
db.clients.remove({_id: ObjectId("4cc45467c55f4d2d2a000002")})
我无法在DOC变量中设置新ID,以插入将镜像旧文档的新文档。
谢谢
PS: I only have to do this due to business requirements
I'm able to achieve it using mongosh, but since there are multiple records to be updated, I'm trying to implement a simple python script to automate the task.
Is it possible to do this with pymongodb?
// store the document in a variable
doc = db.clients.findOne({_id: ObjectId("4cc45467c55f4d2d2a000002")})
// set a new _id on the document
doc._id = ObjectId("4c8a331bda76c559ef000004")
// insert the document, using the new _id
db.clients.insert(doc)
// remove the document with the old _id
db.clients.remove({_id: ObjectId("4cc45467c55f4d2d2a000002")})
I'm not able to set the new Id in the doc variable in order to insert the new document that will mirror the old one.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是Pymongo等效的:
Here's a pymongo equivalent: