简体   繁体   中英

MongoDB update $ update operator with multiple array selector

I'm trying to update an element inside an array using the $ update operator. The document contains 2 array fields and I have to query both to select the correct document.

The collection is called locations and the document looks like this:

{
    "_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"
    ]
}

The update operator looks like this:

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

So I would like to have the element in the wallpapers array replaced. However, the result I get is:

{
    "_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"
    ]
}

So it replaces the wrong element. It seems that the position .$ refers to is the one from the selector of the ancestors field.

Is this a bug or a limitation? Is there a solution (eg anything like .$1 and .$2 ?

I'm using MongoDB 3.2.6.

In your update operation query , use the dot notation as:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM