Mulagala
Mulagala

Reputation: 8661

How to update an array of multiple elements in mongodb..?

I have a requirement to update an array of multiple elements.My collections is in the following way

{
"_id" : ObjectId("53e87e239ae974e6a0a81004"),
"name" : "mulagala",
"notifications" : [
    {
        "name" : "apple",
        "status" : 0
    },
    {
        "name" : "microsoft",
        "status" : 0
    },
    {
        "name" : "android",
        "status" : 0
    }
]
}

now i want to change the every status element of the array should be changed to 1, ie.status:1 with a single query.

I tried in the following way

db.mystatus.update({'notifications.status':0},{$set:{'notifications.$.status':1}},false,true)

But the first record only updating, what to do.Any help would be appriciated!

Upvotes: 1

Views: 69

Answers (1)

Saket
Saket

Reputation: 3129

Have you tried updating the elements of the array using the $ operator for arrays? Currently it updates only one element as the index is coded to 0.

Upvotes: 1

Related Questions