views:

35

answers:

1

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?

+1  A: 

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

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

This updates all matches.

kristina
Sorry, could you be more specific? Say that I pull 10 objects from the database, each with a unique `_id`, and I change one field in each. How do I then put those changes in the database? Bonus points for both simplicity and query efficiency.
Trevor Burnham
Are each of the changes different? If so, you'll have to do separate updates. You can't batch them, but I doubt that being unable to send them together is a bottleneck.
kristina
Ah, I see. Thanks.
Trevor Burnham