How to limit the number of document updates?

Hi
I want to limit the number of document updates in one command.

for example

db.users.updateMany(
    <filter>,
    <update>,
    {
        limit : 100
    }
);

https://jira.mongodb.org/browse/SERVER-55967

Check this link.

Thanks Ramachandra_Tummala.
This is not my answer but this post is near.
actually , i want update only 100 document in one command in php app.

Like Limit clause in MariaDB

For example:
i have 7 documents in tmp collection and have 3 server that want connect to the MongoDB server for run that command(one update command).

{
   "_id"  : 1,
   "name" : "A",
},
{
   "_id"  : 2,
   "name" : "B",
},
{
   "_id"  : 3,
   "name" : "C",
},
{
   "_id"  : 4,
   "name" : "D",
},
{
   "_id"  : 5,
   "name" : "E",
},
{
   "_id"  : 6,
   "name" : "F",
},
{
   "_id"  : 7,
   "name" : "G",
}

In a hypothesis concurrency (limit of update is 3):

          Time  ****************************** [id of docs modified]
          |SV1  -----[Update CMD]------------>     1,2,4
  Requests|SV2  ----------[Update CMD]------->       5
          |SV3  -------[Update CMD]---------->     3,6,7

Do you have not any other solution?

Please vote … :point_down:

1 Like

These code doesn’t works
Update many doesn’t work wit limit

Could try something like this:

Use the $merge operator and have a limit in the pipeline:

db.getCollection("Test").aggregate([
{
    $sort:{
        name:1
    }
},
{
    $limit:3
},
{
    $addFields:{
        active:true
    }
},
{
    $merge:{
        into:'Test',
        on:'_id',
        whenMatched:'merge'
    }
}
])

Edit: 4.4 introduced the merge into same collection which is needed.

2 Likes