Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple simultaneous updates with MongoDB/PyMongo?

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?

like image 929
Trevor Burnham Avatar asked Jun 29 '10 21:06

Trevor Burnham


2 Answers

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

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

This updates all matches.

like image 130
kristina Avatar answered Oct 13 '22 22:10

kristina


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

like image 41
wudpecker Avatar answered Oct 13 '22 21:10

wudpecker