Pergunta

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?

Foi útil?

Solução

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

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

This updates all matches.

Outras dicas

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top