My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

How to update multiple array elements in MongoDB?

karthik's photo
karthik
·Jan 18, 2017

Here is a snippet:

{
        _id : ObjectId("587e2be4411f058ab566d7dc")
    "body" : [ 
        {
                         "hidden" : false,
            "viewed" : {
                "sub" : false,
                "main" : false
            }
        },
            "hidden" : false,
            "viewed" : {
                "sub" : false,
                "main" : false
            }
        }
    ]
}

.update({_id : ObjectId("587e2be4411f058ab566d7dc")},{$set:{"body.$.viewed.main":true}},{multi : true});

This particular query always updates first matched element in the array.But i want to update all matched elements in array.