MongoDB更新深层阵列 [英] MongoDB Update Deep Array

查看:58
本文介绍了MongoDB更新深层阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的mongo数据库中有一个名为music的对象. 我想更新流派是垃圾的地方
乐队的名字是涅rv乐队
专辑名称是Nevermind
曲目顺序为1

,然后将曲目的名称更改为像青少年精神"一样闻起来!".
我已经尝试过与位置运算符一起玩,但是不能完全 弄清楚这个.

{
    genre : "Grunge",
    bands : [ {
        name : "Nirvana",
        albums : [ {
            name : "Nevermind",
            tracks : [ {
                name : "Smell Like Teen Spirit",
                order : 1,
                duration : 301
            },
            {
                name : "In Bloom",
                order : 2,
                duration : 254                                                  
            } ]
        },
        {
            name : "In Utero",
            tracks : [ {
                name : "Server the Servants",
                order : 1,
                duration : 216
            },
            {
                name : "Scentless Apprentice",
                order : 2,
                duration : 254                                                  
            } ]
        } ]
    },
    {
        name : "Karma++ : A Nirvina Tribute Band",
        albums : [ {
            name : "Nevermind",
            tracks : [ {
                name : "Smell Like Teen Spirit",
                order : 1,
                duration : 301
            },
            {
                name : "In Bloom",
                order : 2,
                duration : 254                                                  
            } ]
        },
        {
            name : "In Utero",
            tracks : [ {
                name : "Server the Servants",
                order : 1,
                duration : 216
            },
            {
                name : "Scentless Apprentice",
                order : 2,
                duration : 254                                                  
            } ]
        } ]
    } ]
}

解决方案

不幸的是,目前每次更新只能使用单个"$"位置.这将更新限制为单个嵌入式阵列,类似于文档中的示例: http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positionaloperator (从您的帖子中看来,您已经找到了此信息,但是我包含了该链接,以使其他阅读此帖子的用户受益.)

为了进行更新,您将必须知道以下三个中的两个的位置:乐队在"bands"数组中的位置,专辑在专辑数组中的位置或位置轨道"数组中轨道的位置.

对此功能有一个功能请求,它的版本为2.3.0(尽管可能会更改).
https://jira.mongodb.org/browse/SERVER-831 位置运算符匹配嵌套数组"

暂时,您将不得不知道三个数组中两个数组中子文档的位置:

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!"}})

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

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 Nevermind
The track order is 1

and change the track's name to "Smells Like Teen Spirit!".
I've tried playing with the positional operator, but can't quite figure this out.

{
    genre : "Grunge",
    bands : [ {
        name : "Nirvana",
        albums : [ {
            name : "Nevermind",
            tracks : [ {
                name : "Smell Like Teen Spirit",
                order : 1,
                duration : 301
            },
            {
                name : "In Bloom",
                order : 2,
                duration : 254                                                  
            } ]
        },
        {
            name : "In Utero",
            tracks : [ {
                name : "Server the Servants",
                order : 1,
                duration : 216
            },
            {
                name : "Scentless Apprentice",
                order : 2,
                duration : 254                                                  
            } ]
        } ]
    },
    {
        name : "Karma++ : A Nirvina Tribute Band",
        albums : [ {
            name : "Nevermind",
            tracks : [ {
                name : "Smell Like Teen Spirit",
                order : 1,
                duration : 301
            },
            {
                name : "In Bloom",
                order : 2,
                duration : 254                                                  
            } ]
        },
        {
            name : "In Utero",
            tracks : [ {
                name : "Server the Servants",
                order : 1,
                duration : 216
            },
            {
                name : "Scentless Apprentice",
                order : 2,
                duration : 254                                                  
            } ]
        } ]
    } ]
}

解决方案

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!"}})

这篇关于MongoDB更新深层阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆