简体   繁体   English

使用pymongo更新所有文件并从mongodb返回所有匹配的文档

[英]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.

相关问题 使用Python和PyMongo从所有MongoDB文档中删除属性 - Remove attribute from all MongoDB documents using Python and PyMongo 分组依据仅使用pymongo返回mongodb中的第一个匹配文档,如何检索所有匹配文档? - Group by returns only the first matching document in mongodb using pymongo, How to retrieve all the matching documents? Pymongo 不修改所有匹配的文档 - Pymongo not modifying all matching documents 如何使用 python mongodb 客户端库 (pymongo) 更新 mongodb 集合中所有文档的字段“类型” - How to update field “type” for all documents in mongodb collection using python mongodb client library (pymongo) 使用PyMongo从MongoDB中的数据库检索所有集合的所有文档 - Retrieving all documents of all collections from a database in MongoDB with PyMongo 如何快速获取MongoDB pymongo的所有文件 - How to quickly fetch all documents MongoDB pymongo 使用Pymongo获取集合的所有文档 - Get all documents of a collection using Pymongo 使用 PyMongo 使用自己的值更新所有 MongoDB 字段 - Update all MongoDB fields with their own values with PyMongo 如何在mongodb(pymongo)中查询所有关键字都存在于字段中的文档? - How to query documents in mongodb (pymongo) where all keywords exist in a field? 使用pymongo读取和更新mongodb文档的最佳方法 - Best way to read and update mongodb documents using pymongo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM