[SERVER-26739] Positional update operator updating wrong field Created: 24/Oct/16  Updated: 16/Nov/21  Resolved: 24/Oct/16

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 3.2.6
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: seba kerckhof Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Steps To Reproduce:

Step 1. Make a collection called 'locations'
Step 2. Insert this document in the locations collection:

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

Step 3. Run this query:

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

Step 4
Document is now:

{
    "_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 the `$` operator uses the index of the `ancestor` part of the update operation query, instead of the `wallpapers` one.

Participants:

 Description   

If my update operation query tries to match more than one array field, then the `$` update operator doesn't indicate the correct field.



 Comments   
Comment by Ramon Fernandez Marina [ 24/Oct/16 ]

Hi seba, this is explained in the documentation:

the positional $ operator acts as a placeholder for the first element that matches the query document, and

Your query has two arrays, so the $ operator uses the index of the first element in the query, which in this schema is always going to be the ancestors array.

Regards,
Ramón.

Generated at Thu Feb 08 04:13:04 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.