How to change value?

{
  "_id": 1,
  "skills": [
    {
      "uid": 1000,
      "grade": 1,
      "count": 1
    },
    {
      "uid": 2000,
      "grade": 2,
      "count": 1
    },
    {
      "uid": 3000,
      "grade": 10,
      "count": 2
    }
  ]
}

This is my data. I want to change “count” of two uid (1000,3000) count of uid=1000 is 777, and count of uid = 2000 is 888. how to change at once.


db.nonames.updateOne(
    {_id:1},
    { $set : {"skills.$[elem].count" : [100,200] }},
    { arrayFilters :[ {"elem.uid":{"$in":[1000,3000]}}  ]}
    )

My query is not good and not working.

If you want to modify more than a single element, you simply need multiple arrayFilters, one for each element you want to modify. If I understand your situation correctly you may try the following untested code to see if it works.

uid_1 = 1000
count_1 = 777
uid_2 = 2000
count_2 = 888
db.nonames.updateOne(
    { "_id" : 1 } ,
    { "$set" : {
        "skills.$[one].count" : count_1 ,
        "skills.$[two].count" : count_2
    } } ,
    { "arrayFilters" : [
        { "one.uid" : uid_1 } ,
        { "two.uid" : uid_2 }
    ] }
) ;

While at it, could you please provide followup and potential mark as solve your thread

2 Likes

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.