1

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!

Mulagala
  • 8,231
  • 11
  • 29
  • 48
  • This is a duplicate of [this question](http://stackoverflow.com/questions/4669178/how-to-update-multiple-array-elements-in-mongodb). Short answer is that this cannot be done using the positional operator `$`. There's [this issue](http://jira.mongodb.org/browse/SERVER-1243) in JIRA you should vote for if you want this implemented. Answer in other question gives you an idea of how to go around this issue. – Juan Carlos Farah Aug 11 '14 at 13:58

1 Answers1

1

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.

Saket
  • 3,079
  • 3
  • 29
  • 48
  • @Seket, Tried that one also, but like you said only one element is updating, i need to update all the elements in the list – Mulagala Aug 11 '14 at 09:59
  • 2
    @Seket, this won't work because the positional `$` operator only matches the **first element** that matches the query document. For more info, see [here](http://docs.mongodb.org/manual/reference/operator/update/positional/#up._S_). – Juan Carlos Farah Aug 11 '14 at 14:01