Updating nested arrays in mongoDB via mongo shell [duplicate]

You are directly hitting one of the current limitations of MongoDB.
The problem is that the engine does not support several positional operators.
See this Multiple use of the positional `$` operator to update nested arrays

There is an open ticket for this: https://jira.mongodb.org/browse/SERVER-831 (mentioned also there)

You can also read this one on how to change your data model: Updating nested arrays in mongodb

If it is feasible for you, you can do:

db.collection.update({_id:2,"event_type.name":'MT' ,"event_type.language.name":'English'},{$set:{"event_type.0.language.$.count":<number>}})

db.collection.update({_id:2,"event_type.name":'MT' ,"event_type.language.name":'English'},{$set:{"event_type.$.language.0.count":<number>}})

But you cannot do:

db.collection.update({_id:2,"event_type.name":'MT' ,"event_type.language.name":'English'},{$set:{"event_type.$.language.$.count":<number>}})

Leave a Comment