How to limit number of updating documents in mongodb

How to implement somethings similar to db.collection.find().limit(10) but while updating documents?

Now I'm using something really crappy like getting documents with db.collection.find().limit() and then updating them.

In general I wanna to return given number of records and change one field in each of them.

Thanks.


Solution 1:

You can use:

db.collection.find().limit(NUMBER_OF_ITEMS_YOU_WANT_TO_UPDATE).forEach(
    function (e) {
        e.fieldToChange = "blah";
        ....
        db.collection.save(e);
    }
);

(Credits for forEach code: MongoDB: Updating documents using data from the same document)

What this will do is only change the number of entries you specify. So if you want to add a field called "newField" with value 1 to only half of your entries inside "collection", for example, you can put in

db.collection.find().limit(db.collection.count() / 2).forEach(
    function (e) {
        e.newField = 1;
        db.collection.save(e);
    }
);

If you then want to make the other half also have "newField" but with value 2, you can do an update with the condition that newField doesn't exist:

db.collection.update( { newField : { $exists : false } }, { $set : { newField : 2 } }, {multi : true} );

Solution 2:

Using forEach to individually update each document is slow. You can update the documents in bulk using

ids = db.collection.find(<condition>).limit(<limit>).map(
    function(doc) {
        return doc._id;
    }
);
db.collection.updateMany({_id: {$in: ids}}, <update>})

Solution 3:

The solutions that iterate over all objects then update them individually are very slow.

Retrieving them all then updating simultaneously using $in is more efficient.

ids = People.where(firstname: 'Pablo').limit(10000).only(:_id).to_a.map(&:id)
People.in(_id: ids).update_all(lastname: 'Cantero')

The query is written using Mongoid, but can be easily rewritten in Mongo Shell as well.

Solution 4:

Unfortunately the workaround you have is the only way to do it AFAIK. There is a boolean flag multi which will either update all the matches (when true) or update the 1st match (when false).

Solution 5:

As the answer states there is still no way to limit the number of documents to update (or delete) to a value > 1. A workaround to use something like:

db.collection.find(<condition>).limit(<limit>).forEach(function(doc){db.collection.update({_id:doc._id},{<your update>})})