Updating embedded document property in Mongodb

Viewed 32981

I have a document that looks like this:

{
    "_id": 3,
    "Slug": "slug",
    "Title": "title",
    "Authors": [
        {
            "Slug": "slug",
            "Name": "name"
        }
    ]
}

I want to update all Authors.Name based on Authors.Slug. I tried this but it didn't work:

.update({"Authors.Slug":"slug"}, {$set: {"Authors.Name":"zzz"}});

What am I doing wrong here?

3 Answers

yes, Rock's solution is working, P.S Notes is really helpful when trying Robo31.. If we want to update all db.collection_name.update({}, {$set: {"Authors.$[].Name": "zzz"}})

If we want to update with matching object in an array db.collection_name.update({}, {$set: {"Authors.$[i].Name": "zzz"}}, {arrayFilters: [{"i.Slug": "slug"}]})

Ref: https://jira.mongodb.org/browse/SERVER-1243

Related