使用 MongoDB/PyMongo 进行多个同时更新?

发布于 2024-09-07 08:51:11 字数 216 浏览 9 评论 0原文

根据 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空城之時有危險 2024-09-14 08:51:11

实际上,您可以使用 multi 选项更新多个文档:

collection.update(spec, doc, multi=True)

这会更新所有匹配项。

Actually, you can update multiple docs with the multi option:

collection.update(spec, doc, multi=True)

This updates all matches.

呆头 2024-09-14 08:51:11

您可以使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文