0

I tried to update the array inside the embedded document field. but it is not working how to achieve this scenario.

data_Collection

{
    "id":101,
    "review_ratings":[
       {
           id:102,
           name:"hari"
       },{
           id:103,
           name:"mani"
       }
    ]
}

How to update id 103 name field

Ankur
  • 496
  • 1
  • 6
  • 11
hari prasanth
  • 716
  • 1
  • 15
  • 35

2 Answers2

0

You should use positional operators

db.test.update({"id":101, "review_ratings.id": 103},
               {$set: {"review_ratings.$.name":"mohit"}});
Mohit Mutha
  • 2,921
  • 14
  • 25
0
You can use aggregate function for searching and can use find and update method like:-

test.aggregate(
                [
                  { $match: {} },
                  {$project: {
                    review_ratings: {
                      $filter: {
                        input: '$review_ratings',
                        as: 'review_ratings',
                        cond: { $wq: [ '$$review_ratings.id', '103' ] }
                      }
                    }
                   }
                  }

                ],
                (err, res) => {

                });
vimmi
  • 407
  • 1
  • 5
  • 14