Update Multiple Array Elements Using greaterthanorequal lessthanorequal in Mongo: Updating multiple elements in Array data type in MongoDB using the shell.

We have 2 documents in MongoDB like below

/* 1 */
{
  "_id" : ObjectId("5b594b462fe5553ce8071c27"),
  "grades" : [100, 200, 300]
}
/* 2 */
{
  "_id" : ObjectId("5b594b712fe5553ce8071c28"),
  "grades" : [400, 500, 100]
}

now we want to update grades value of which are greater than or equal to 100 is the Query for updating multiple elements in Array.

 

db.dummy_collection.update(
   { grades: { $gte: 100 } },
   { $set: { "grades.$" : 80 } },
   {
     multi: true,
    }
)

In the above Query all the elements which are greater than or equal 100 are replaced with 80 we have a list of operators show here and dummy_collection is my Collection name.

Also Read: How to Update Arrays Element in MongoDB

Update Multiple Array Elements Using greaterthanorequal lessthanorequal in Mongo

Leave a Reply

Your email address will not be published. Required fields are marked *