Update object inside the array in mongoDb using mongoose

Viewed 6025

I am working on MongoDB for update object value which is inside of the array in MongoDB collection.

My collection is like

{
  "_id": ObjectId("59b7e839200a5c00ee2d2851"),
  "player": "New",
  "playesList": [
    {
      "_id": ObjectId("59b2a4f749fee40959e556d3"),
      "name": "abcd",
    },
    {
      "_id": ObjectId("59b2a4f749fee40959e556d4"),
      "name": "pqrs",
    }
  ]
}

Now I want to update the name of the player whose id is 59b2a4f749fee40959e556d3(i mean first player name currently it was abcd), whose collection id is 59b7e839200a5c00ee2d2851 and player New.

So I am trying to update with this query

play.update(
      {
          '_id': '59b7e839200a5c00ee2d2851',
          'player': 'new',
          'playesList._id': '59b2a4f749fee40959e556d3'
      },
      {
          '$set': { 'playesList.$.name': 'wxyz' }
      },
      function(error, success) {
         console.log(error, success);
      }
)

But here I got in console like null { ok: 1, nModified: 0, n: 0 } and value cann't update into collection. Please help me how can solve this error. Thank you in advance.

2 Answers
Related