使用 MongoDB/PyMongo 进行多个同时更新?
根据 PyMongo 文档, update( )
一次只能更新一个文档。假设我想要同时更新 100 个文档。这是很大的开销。有没有办法通过 PyMongo 使用单个 MongoDB 查询更新多个文档?
According to the PyMongo docs, update()
can only update a single document at a time. Let's say I have 100 documents I want to update simultaneously. That's a lot of overhead. Is there a way to update multiple documents with a single MongoDB query through PyMongo?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,您可以使用 multi 选项更新多个文档:
这会更新所有匹配项。
Actually, you can update multiple docs with the multi option:
This updates all matches.
您可以使用 mongodb 2.6 中提供的批量写入功能一次更新具有不同 _id 的多个文档
试试这个 http://api.mongodb.org/python/current/examples/ 准确地说,
您可以使用有序批量写入操作来更新具有不同条件的大量记录。
查看此以了解更多详细信息 最佳方法使用 pymongo 读取和更新 mongodb 文档
you can update multiple documents with different _id at a time by using bulk write feature available in mongodb 2.6
try this http://api.mongodb.org/python/current/examples/bulk.html
in precise you can use Ordered Bulk Write Operations which updates a bulk of records which are with different criteria.
view this for more details Best way to read and update mongodb documents using pymongo