Как найти и обновить конкретный объект из массива (mon goose) в node js - PullRequest
0 голосов
/ 07 мая 2020

Я хочу обновить значение 'Name' из массива объекта (myArr []), используя Arr_id.

mydb:{
     "_id" : ObjectId("5eb2b06d626fc539172013f8"),
     "is_deleted" : false,
     "email" : "rajnish@dresma.com",
     "myArr": [
         {"Arr_id":"5eb2b06d626fc539172013f7",
          "Name":"Aman"
          },
         {"Arr_id":"5eb2b06d626fc539172001k9",
          "Name":"Ram"
          },
         {"Arr_id":"5eb2b06d626fc539172013k4",
          "Name":"Piyush"
          }
        ]
     }

1 Ответ

1 голос
/ 07 мая 2020
You can update in nested objects using $ positional operator.
here , Id is the id you want to update the value of name.
db.collection.update(
    { "myArr.Arr_id": Id }, 
    { "$set": { "myArr.$.Name": "SHYam" } }

)

...