[SERVER-7248] update of element of a sublist updates the wrong element depending on the order the request was built Created: 03/Oct/12  Updated: 07/Mar/14  Resolved: 03/Oct/12

Status: Closed
Project: Core Server
Component/s: JavaScript, Write Ops
Affects Version/s: 2.2.0
Fix Version/s: None

Type: Bug Priority: Critical - P2
Reporter: Benoît Fonty Assignee: Unassigned
Resolution: Done Votes: 0
Labels: update
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Linux


Attachments: File test.js    
Operating System: Linux
Participants:

 Description   

Here are the data before update

{
        "_id" : ObjectId("506c614a80178fe821c091bf"),
        "identifier" : "1",
        "list" : [
                {
                        "identifier" : {
                                "id" : "1",
                                "type" : "raw"
                        },
                        "name" : "name1"
                },
                {
                        "identifier" : {
                                "id" : "2",
                                "type" : "raw"
                        },
                        "name" : "name2"
                }
        ]
}

I try to update the second element

db.test.update({identifier:"1","list.identifier.id":"2","list.identifier.type":"raw"},{$set:{"list.$.name":"name22"}},false,true);

...but mongo updated the first

{
        "_id" : ObjectId("506c614a80178fe821c091bf"),
        "identifier" : "1",
        "list" : [
                {
                        "identifier" : {
                                "id" : "1",
                                "type" : "raw"
                        },
                        "name" : "name22"
                },
                {
                        "identifier" : {
                                "id" : "2",
                                "type" : "raw"
                        },
                        "name" : "name2"
                }
        ]
}

This time I give a different order for the request

db.test.update({identifier:"1","list.identifier.type":"raw","list.identifier.id":"2"},{$set:{"list.$.name":"name222"}},false,true);

...and mongo updated the correct element

{
        "_id" : ObjectId("506c614a80178fe821c091bf"),
        "identifier" : "1",
        "list" : [
                {
                        "identifier" : {
                                "id" : "1",
                                "type" : "raw"
                        },
                        "name" : "name22"
                },
                {
                        "identifier" : {
                                "id" : "2",
                                "type" : "raw"
                        },
                        "name" : "name222"
                }
        ]
}



 Comments   
Comment by Scott Hernandez (Inactive) [ 03/Oct/12 ]

You need to use $elemMatch to only match the correct array element. The current query you have matches both array element in the document; and therefor the update matches one of the two array elements, not just one.

http://www.mongodb.org/display/DOCS/Advanced%20Queries#AdvancedQueries-%24elemMatch

Generated at Thu Feb 08 03:14:00 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.