MongoDB使用多个数组选择器更新$ update运算符

时间:2016-10-24 11:41:18

标签: arrays mongodb

我正在尝试使用$更新运算符更新数组中的元素。该文档包含2个数组字段,我必须查询两个以选择正确的文档。

该集合名为locations,文档如下所示:

{
    "_id" : "XqEQYpitGFG3nnf3C",
    "wallpapers" : [ 
        {
            "_metadata" : {
                "master" : "vwb22W4MhkqtvAp89",
                "isMaster" : false
            },
            "role" : "master",
            "_id" : ""
        }, 
        {
            "_metadata" : {
                "master" : "vwb22W4MhkqtvAp89",
                "isMaster" : false
            },
            "role" : "clone",
            "_id" : ""
        }, 
        {
            "_metadata" : {
                "master" : "vwb22W4MhkqtvAp89",
                "isMaster" : false
            },
            "role" : "pod",
            "_id" : ""
        }
    ],
    "ancestors" : [ 
        "vwb22W4MhkqtvAp89", 
        "tqzqfum9uMs47xcHW", 
        "b4d83aqTkq6TGvXts", 
        "XqEQYpitGFG3nnf3C"
    ]
}

更新运算符如下所示:

db.getCollection('locations').update(
    {
        "ancestors": "b4d83aqTkq6TGvXts",
        "wallpapers": {
            "$elemMatch": {
                "role": "clone",
                "_metadata.master": "vwb22W4MhkqtvAp89"
            }
        }
    },
    {
        "$set": {
            "wallpapers.$": {
                "_id": "D33WNZh7Bg4itPdhk",
                "_metadata": {
                    "master": "b4d83aqTkq6TGvXts",
                    "isMaster": false
                },
                "role": "clone"
            }
        }
    }
)

所以我希望替换wallpapers数组中的元素。但是,我得到的结果是:

{
    "_id" : "XqEQYpitGFG3nnf3C",
    "wallpapers" : [ 
        {
            "_metadata" : {
                "master" : "vwb22W4MhkqtvAp89",
                "isMaster" : false
            },
            "role" : "master",
            "_id" : ""
        }, 
        {
            "_metadata" : {
                "master" : "vwb22W4MhkqtvAp89",
                "isMaster" : false
            },
            "role" : "clone",
            "_id" : ""
        }, 
        {
            "_id" : "D33WNZh7Bg4itPdhk",
            "_metadata" : {
                "master" : "b4d83aqTkq6TGvXts",
                "isMaster" : false
            },
            "role" : "clone"
        }
    ],
    "ancestors" : [ 
        "vwb22W4MhkqtvAp89", 
        "tqzqfum9uMs47xcHW", 
        "b4d83aqTkq6TGvXts", 
        "XqEQYpitGFG3nnf3C"
    ]
}

所以它取代了错误的元素。 .$所指的位置似乎是来自ancestors字段的选择器的位置。

这是一个错误还是一个限制?是否有解决方案(例如.$1.$2

之类的解决方案

我正在使用MongoDB 3.2.6。

1 个答案:

答案 0 :(得分:1)

update operation query中,使用dot notation作为:

db.getCollection('locations').update(
    {
        "ancestors": "b4d83aqTkq6TGvXts",
        "wallpapers.role": "clone", // <--- dot notation
        "wallpapers._metadata.master": "vwb22W4MhkqtvAp89" // <-- dot notation
    },
    {
        "$set": {
            "wallpapers.$": {
                "_id": "D33WNZh7Bg4itPdhk",
                "_metadata": {
                    "master": "b4d83aqTkq6TGvXts",
                    "isMaster": false
                },
                "role": "clone"
            }
        }
    }
)