0

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 (e.g. anything like .$1 and .$2 ?

I'm using MongoDB 3.2.6.

1 Answer 1

1

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"
            }
        }
    }
)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that seems to work. But do you know why it didn't work with $elemMatch, but does work with your version?
The update operation with dot notation works because $elemMatch requires the same nested elements to have the values and using dot notation allows for any nested elements to have any values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.