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!

1
  • This is a duplicate of this question. Short answer is that this cannot be done using the positional operator $. There's this issue 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. Commented Aug 11, 2014 at 13:58

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@Seket, Tried that one also, but like you said only one element is updating, i need to update all the elements in the list
@Seket, this won't work because the positional $ operator only matches the first element that matches the query document. For more info, see here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.