[SERVER-39783] Updating Element in the Array using the positional $ operator Created: 23/Feb/19  Updated: 26/Feb/19  Resolved: 26/Feb/19

Status: Closed
Project: Core Server
Component/s: Internal Code
Affects Version/s: 3.6.2
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Venkatesh Assignee: Eric Sedor
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Steps To Reproduce:
  • Steps to reproduce the problem
  • Steps to show changing field name makes it work as expected. Note the only change is array element field name is changed from "s" to "a"

Tried on MongoDB server version: 3.6.2

Steps to reproduce the problem.

// code placeholder
> db.mycoll.insert({"_id":1, "arr":[{"s": "ABC", "c":"123", "count": 1}, {"s": "ABC", "c":"12345", "count": 2}]})
 
WriteResult({ "nInserted" : 1 })
 
> db.mycoll.find().pretty()> db.mycoll.find().pretty()
{ 
     "_id" : 1, 
     "arr" : [ 
               { 
                 "s" : "ABC", 
                 "c" : "123", 
                 "count" : 1 
               }, 
               { 
                 "s" : "ABC", 
                 "c" : "12345", 
                 "count" : 2 
               } 
           ]
}
 
> db.mycoll.update({ "_id" : 1, "arr.s": "ABC", "arr.c":"12345"}, { "$set" : { "arr.$" : { "s" : "ABC", "c" : "12345", "count" : 5 }}})
 
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
 
> db.mycoll.find().pretty()
 
{
	"_id" : 1,
	"arr" : [
		{
		  "s" : "ABC",
                  "c" : "12345", // *** wrong array element updated
                  "count" : 5   // *** wrong array element updated
                 },
                 {
                   "s" : "ABC",
                   "c" : "12345",
                   "count" : 2
                  }
                 ]
}

 

Steps to show changing field name makes it work as expected. Note the only change is array element field name is changed from "s" to "a"

> db.mycoll.insert({"_id":1, "arr":[{"a": "ABC", "c":"123", "count": 1}, {"a": "ABC", "c":"12345", "count": 2}]})
 
WriteResult({ "nInserted" : 1 })
 
> db.mycoll.find().pretty()
{ 
       "_id" : 1, 
       "arr" : [ 
                 { 
                   "a" : "ABC", 
                   "c" : "123", 
                   "count" : 1 
                 }, 
                 { 
                   "a" : "ABC", 
                   "c" : "12345", 
                   "count" : 2 
                  } 
                ]
}
 
> db.mycoll.update({ "_id" : 1, "arr.a": "ABC", "arr.c":"12345"}, { "$set" : { "arr.$" : { "a" : "ABC", "c" : "12345", "count" : 5 }}})
 
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
 
> db.mycoll.find().pretty()
 
{ 
       "_id" : 1, 
       "arr" : [ 
                 { 
                   "a" : "ABC", 
                   "c" : "123", 
                   "count" : 1 
                 }, 
                 { 
                  "a" : "ABC", 
                  "c" : "12345", // *** correctly updated
                  "count" : 5 // *** correctly updated
                 } 
               ]
}

 

 

 

Participants:

 Description   

MongoDB Update is working inconsistently when using the positional $ operator to update an array element. It seems to be always updating the first element and not the element matching the query condition.

Found that update is working as expected when the array element field name is changed from "s" to "a". Please see steps to reproduce for more details.

Found the issue on MongoDB server version: 3.6.2. Not sure if it is present in all versions.



 Comments   
Comment by Eric Sedor [ 26/Feb/19 ]

Hi, thanks for writing in. $elemMatch should help you target specific array elements more accurately.

For example, per this documentation the following should work:

db.mycoll.update({ "_id" : 1, arr: {"$elemMatch": {"s": "ABC", "c":"12345"}}}, { "$set" : { "arr.$.s" : "ABC", "arr.$.c" : "12345", "arr.$.count" : 5 }})

For further questions about using specific MongoDB features, please post on the mongodb-user group or Stack Overflow with the mongodb tag.

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