[英]Update ALL and return ALL matching documents from mongodb using pymongo
I want to fetch all updated documents from MongoDB using pymongo. 我想使用pymongo从MongoDB获取所有更新的文档。
I tried using findOneAndUpdate() but it only updates one document. 我尝试使用findOneAndUpdate(),但它仅更新一个文档。 Also tried using MongoDB js server function, but it is too slow and mostly not recommended. 也尝试过使用MongoDB js服务器功能,但是它太慢了,通常不建议这样做。
db.system.js.save({
_id: "distributedTaskQueue",
value : function(coll,status,limit,fromStatus,toStatus) {
records = db.getCollection(coll).find({status:fromStatus}).limit(limit)
results.forEach (function(record){
record[status]= toStatus
db.getCollection(coll).save(record);
})
return records;
}
})
The idea is to run this on 100 servers using pymongo to fetch documents for processing. 这个想法是在使用pymongo来获取文档进行处理的100台服务器上运行它。
You're looking for update_many
- http://api.mongodb.com/python/current/api/pymongo/collection.html#pymongo.collection.Collection.update_many 您正在寻找update_many
- http: update_many
Find by the status
and use $set
to update with the toStatus
. 按status
查找并使用$set
使用toStatus
更新。
Manually iterating over the documents will be slow and inefficient. 手动遍历文档将很慢且效率低下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.