'Update Query in MongoDB with where conditions

{
"_id" : ObjectId("510902fb7995fe3504000002"),
"name" : "Gym",
"status" : "1",
"whichs" : [
    {
        "name" : "American",
        "status" : "1"
    }
]
}

Above is my collection object.. I want to update which.name to "Indian" where which.status = 1.. Please help me with the mongoDB query for doing this..



Solution 1:[1]

You can try this:

db.collection.update({"whichs.status" : "1" }, 
                     {$set : { "whichs.$.name" : "Indian"},
                     false,
                     true);

This finds "whichs.status" : "1" then sets "whichs.$.name" : "Indian" for all documents. For more info on the update() method, read here.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andy Refuerzo