Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating nested arrays in mongoDB via mongo shell [duplicate]

Tags:

mongodb

Following is a MongoDB document:

{
    "_id" : 2,
    "mem_id" : M002,
    "email" : "[email protected]",
    "event_type" : [ 
        {
            "name" : "MT",
            "count" : 1,
            "language" : [ 
                {
                    "name" : "English",
                    "count" : 1,
                    "genre" : [ 
                        {
                            "name" : "Action",
                            "count" : 6
                        }, 
                        {
                            "name" : "Sci-Fi",
                            "count" : 3
                        }
                    ],
                    "cast" : [ 
                        {
                            "name" : "Sam Wortington",
                            "count" : 2
                        }, 
                        {
                            "name" : "Bruce Willis",
                            "count" : 4
                        }, 
                        {
                            "name" : "Will Smith",
                            "count" : 7
                        }, 
                        {
                            "name" : "Irfan Khan",
                            "count" : 1
                        }
                    ]
                }
            ]
        }
    ]
}

I'm not able to update fields that is of type array, specially event_type, language, genre and cast because of nesting. Basically, I wanted to update all the four mentioned fields along with count field for each and subdocuments. The update statement should insert a value to the tree if the value is new else should increment the count for that value.
What can be the query in mongo shell? Thanks

like image 978
Pratik Borkar Avatar asked Sep 02 '13 12:09

Pratik Borkar


People also ask

How do I update a nested array in MongoDB?

Update Nested Arrays in Conjunction with $[]The $[<identifier>] filtered positional operator, in conjunction with the $[] all positional operator, can be used to update nested arrays. The following updates the values that are greater than or equal to 8 in the nested grades. questions array if the associated grades.


1 Answers

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>}})
like image 140
attish Avatar answered Sep 27 '22 21:09

attish