Question

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?

Was it helpful?

Solution

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

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

This updates all matches.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top