[SERVER-23563] $in setting positional operator ($) in an update query Created: 06/Apr/16  Updated: 14/Apr/16  Resolved: 06/Apr/16

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

Type: Bug Priority: Major - P3
Reporter: David Martinho Assignee: Kelsey Schubert
Resolution: Done Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Participants:

 Description   

Consider the following example:

db.getCollection("test").insert({
   "_id" : 5,
   "scopes" : [
       "a",
       "b"
   ],
   "tags" : [
       {
           "name" : "abc",
           "id" : 1
       },
       {
           "name" : "def",
           "id" : 2
       }
   ]
})
 
db.getCollection('test').update({
    "_id" : 5,
    "scopes" : { "$in" : ["a"]},
    "tags.id" : 2
}, {
    "$set" : { "tags.$.name" : "ghi"}
})

This example changes the first tag name abc to ghi instead of the desired behavior of changing the tag with name def to ghi.

I think the $in operator is setting the positional operator $, because if we change the query for "scopes" :

{ "$in" : ["b"]}

, then the second tag is getting updated...



 Comments   
Comment by Kelsey Schubert [ 06/Apr/16 ]

Hi davidmartinho,

Thank you for the report. This behavior is expected and is documented here. In your example, two array elements are being queried. This results in ambiguity that we do not support since only one of these matches sets the value of "$". Consequently, we do not recommend, and soon will not allow (SERVER-14662), queries that traverse multiple arrays.

Kind regards,
Thomas

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