0
votes

I want to fetch all updated documents from MongoDB using pymongo.

I tried using findOneAndUpdate() but it only updates one document. Also tried using MongoDB js server function, but it is too slow and mostly not recommended.

    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.

1

1 Answers

0
votes

You're looking for update_many - http://api.mongodb.com/python/current/api/pymongo/collection.html#pymongo.collection.Collection.update_many

Find by the status and use $set to update with the toStatus.

Manually iterating over the documents will be slow and inefficient.