[SERVER-14281] Positional Operator Points to Wrong Index in $set Update Created: 17/Jun/14  Updated: 17/Jun/14  Resolved: 17/Jun/14

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

Type: Bug Priority: Critical - P2
Reporter: yudho ahmad diponegoro Assignee: Scott Hernandez (Inactive)
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-831 Positional Operator Matching Nested A... Closed
Operating System: ALL
Participants:

 Description   

We have this document

{
        "_id" : ObjectId("539fe24d822ea86809480f0c"),
        "bbs" : [
                {
                        "_id" : ObjectId("539fe24d822ea86809480f06"),
                        "name" : "pelanggan",
                        "bbattributes" : [
                                {
                                        "_id" : ObjectId("539fe24d822ea86809480f07"),
                                        "name" : "nama",
                                        "cell" : "l-0",
                                        "status" : "active"
                                },
                                {
                                        "_id" : ObjectId("539fe24d822ea86809480f08"),
                                        "name" : "dob"
                                }
                        ]
                },
                {
                        "_id" : ObjectId("539fe24d822ea86809480f09"),
                        "name" : "nota",
                        "bbattributes" : [
                                {
                                        "_id" : ObjectId("539fe24d822ea86809480f0a"),
                                        "name" : "tanggal",
                                        "cell" : "r-0",
                                        "status" : "active"
                                },
                                {
                                        "_id" : ObjectId("539fe24d822ea86809480f0b"),
                                        "name" : "lunas"
                                }
                        ]
                }
        ]
}

This update syntax is supposed to update bbattributes whose name is "dob", but instead it updates "nama".

db["apps"].update({"_id": ObjectId('539fe24d822ea86809480f0c'),"bbs._id": ObjectId('539fe24d822ea86809480f06'),"bbs.0.bbattributes._id": ObjectId('539fe24d822ea86809480f08')},{"$set": {"bbs.0.bbattributes.$.status": "active", "bbs.0.bbattributes.$.cell": "l-1"}});

Here is the result after update

{
        "_id" : ObjectId("539fe24d822ea86809480f0c"),
        "bbs" : [
                {
                        "_id" : ObjectId("539fe24d822ea86809480f06"),
                        "name" : "pelanggan",
                        "bbattributes" : [
                                {
                                        "_id" : ObjectId("539fe24d822ea86809480f07"),
                                        "name" : "nama",
                                        "cell" : "l-1",
                                        "status" : "active"
                                },
                                {
                                        "_id" : ObjectId("539fe24d822ea86809480f08"),
                                        "name" : "dob"
                                }
                        ]
                },
                {
                        "_id" : ObjectId("539fe24d822ea86809480f09"),
                        "name" : "nota",
                        "bbattributes" : [
                                {
                                        "_id" : ObjectId("539fe24d822ea86809480f0a"),
                                        "name" : "tanggal",
                                        "cell" : "r-0"
                                },
                                {
                                        "_id" : ObjectId("539fe24d822ea86809480f0b"),
                                        "name" : "lunas"
                                }
                        ]
                }
        ]
}

Why does it update the wrong index, which I suspect to be always the first element?



 Comments   
Comment by Scott Hernandez (Inactive) [ 17/Jun/14 ]

It is on the road-map but not currently scheduled. In the last refactoring of update some work was done to lay down a better foundation but this was not finished enough for the array/positional features.

If you know the full path, using the number ordinals instead of the positional operator (e.g. "bbs.0.bbattributes.1.status"), then you can do the update with any array depth, or nesting.

Comment by yudho ahmad diponegoro [ 17/Jun/14 ]

I see. Thanks for your explanation.
So, currently, if we have more than 1 level deep of arrays, there is no way to do atomic update on the inner array even if we know exactly the path before querying, am I right?
Since the linked ticket is already 4 years old, will MongoDB actually implement this multiple "$" in near future?

Comment by Scott Hernandez (Inactive) [ 17/Jun/14 ]

There are two query criteria which match array elements, and that is the issue here (as well as having a nested array match). Currently there is no support for an ambiguity like this, and only one of the matches sets the value of the "$". in this case to "0", the first match. Until there is support for more than one positional match, and/or nested arrays, this will not be supported, and you should not use either query as they are unsupported/ambiguous for positional updates.

I have linked the issue which would allow you to do what you want.

Comment by yudho ahmad diponegoro [ 17/Jun/14 ]

for information, I used 2.6.0

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