MongoDB Positional operator support in 3.2 ?

48 views
Skip to first unread message

sivapra...@gmail.com

unread,
May 23, 2018, 3:29:09 AM5/23/18
to mongodb-user
Hi, I'm using MongoDB 3.2 and one of my requirements is to update a field in a JSON array. Assume I have the following document:

{
   
"Time" : 1527059445,
   
"UID" : "user-id-1",
   
"Hit" : 23,
   
"Properties" : [
       
{
           
"Key" : "Name",
           
"Value": "John"
       
},
       
{
           
"Key": "Age",
           
"Value": 23
       
}
   
]
}

In MongoDB 3.6, I can do the following to change the `Properties.Value` from "John" to "xyz" if the UID is "user-id-1".

db.getCollection('testcollection').updateMany(
   
{
       
"UID": "user-id-1",
       
"Properties.Key" : "Name"
   
},
   
{
        $set
: {
           
"Properties.$.Value" : "xyz"
       
}
   
}
)

However, when I try to do the same in MongoDB 3.2, I get this error: "WriteError: Invalid BSON field name 'Properties.$.Value'" I have also attached the screenshot of the error message (I'm using Robo 3T 1.2)



MongoDB 3.2 Positional Operator Error.JPG

Kevin Adistambha

unread,
May 24, 2018, 12:31:46 AM5/24/18
to mongodb-user

Hi

I tried the operation you posted on MongoDB 3.2.18 and was able to perform the update with no error in the mongo shell:

> db.version()
3.2.18

> db.test.find()
{
  "_id": ObjectId("5b063da4061942f6f05dc33d"),
  
"Time": 1527059445,
  "UID": "user-id-1",
  "Hit": 23,
  "Properties": [
    {
      "Key": "Name",
      "Value": "John"
    },
    {
      "Key": "Age",
      "Value": 23

    }
  ]
}

> db.test.updateMany(
...     {
...         "UID": "user-id-1",
...         "Properties.Key" : "Name"
...     },
...     {
...         $set : {
...             "Properties.$.Value" : "xyz"
...         }
...     }
... )
{
  "acknowledged": true,
  "matchedCount": 1,
  "modifiedCount": 1
}

> db.test.find()
{
  "_id": ObjectId("5b063da4061942f6f05dc33d"),
  
"Time": 1527059445,
  "UID": "user-id-1",
  "Hit": 23,
  "Properties": [
    {
      "Key": "Name"
,
      "Value": "xyz"

    },
    {
      "Key": "Age",
      "Value": 23
    }
  ]
}

Perhaps it’s an issue specific to Robo3T? If you find that this operation still fails, could you post the output of db.version() and mongod --version?

Best regards
Kevin

Message has been deleted

sivapra...@gmail.com

unread,
May 24, 2018, 5:56:13 AM5/24/18
to mongodb-user
Yep. I checked the version and it is 3.2.0

Kevin Adistambha

unread,
May 29, 2018, 12:00:00 AM5/29/18
to mongodb-user

Hi

Yep. I checked the version and it is 3.2.0

Have you tried the operation with the mongo shell? Was it successful?

Best regards
Kevin

Reply all
Reply to author
Forward
0 new messages