Sign in
Log inSign up

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.

Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct