MongoDB Update Deep Array

前端 未结 1 1424
隐瞒了意图╮
隐瞒了意图╮ 2020-12-03 23:19

I have the following object in my mongo database named music.

I want to update where the genre is Grunge
The band name is Nirvana
The album name is

相关标签:
1条回答
  • 2020-12-03 23:38

    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!"}})
    
    0 讨论(0)
提交回复
热议问题