MongoDB Update Deep Array

Unfortunately, at present it is only possible to use a single “$” positional per update. This limits the update to a single embedded array, similar to the example in the documentation: http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positionaloperator
(From your post, it looks like you have already found this, but I have included the link for the benefit of any other users reading this post.)

In order to make the update, you will have to know the position of two out of the following three: The position of the band in the “bands” array, the position of the album in the albums array, or the position of the track in the “tracks” array.

There is a feature request for this functionality, and it is slated for version 2.3.0 (although this is subject to change).
https://jira.mongodb.org/browse/SERVER-831 “Positional Operator Matching Nested Arrays”

For the time being, you will have to know the position of the sub documents in two out of the three arrays:

db.music.update({genre : "Grunge", "bands.name" : "Nirvana"}, {$set:{"bands.$.albums.0.tracks.0.name":"Smells Like Teen Spirit!"}})

db.music.update({genre : "Grunge", "bands.0.albums.name" : "Nevermind"}, {$set:{"bands.0.albums.$.tracks.0.name":"Smells Like Teen Spirit!"}})

or

db.music.update({genre : "Grunge", "bands.0.albums.0.tracks.order" : 1}, {$set:{"bands.0.albums.0.tracks.$.name":"Smells Like Teen Spirit!"}})

Leave a Comment