1

I am currently working on MongoDB. There is one condition which I can't handle it. So I need help from you.

My collection data is like,

{
  "_id": ObjectId("59bb85ad7a64f91145db564e"),
  "updatedAt": new Date(1505461677518),
  "createdAt": new Date(1505461677518),
  "player_detail": [
    {
      "_id": ObjectId("59bb85ad7a64f91145db564f"),
      "name": "abcd",
      "runs": [
        {
          "innings": "1st",
          "run": "69",
          "wicket": "2"
        },
        {
          "innings": "2nd",
          "run": "98",
          "wicket": "3"
        }
      ]
    },
    {
      "_id": ObjectId("59bb85ad7a64f91145db564g"),
      "name": "new player",
      "runs": [
        {
          "innings": "1st",
          "run": "70",
          "wicket": "0"
        },
        {
          "innings": "2nd",
          "run": "111",
          "wicket": "1"
        }
      ]
    }
  ]
}

Now I want to update player run, here condition is like,

  1. Collection id is 59bb85ad7a64f91145db564e
  2. player name is abcd
  3. innings 1'st

Here collection name is cricket. And I am trying to update it using query like,

cricket.findOneAndUpdate(
{
  $and: [
    { _id: mongoose.Types.ObjectId('59bb85ad7a64f91145db564e') },
    { "player_detail.name": 'abcd' }, 
  ],
},
{
  "player_detail.runs.run": '98',
},
function(error, response) {
  console.log("response", response);
}

here I get a response like { MongoError: cannot use the part (player_detail of player_detail.name) to traverse the element....

So please help me how can I update run of player which has fulfilled above three conditions. Thank you in advance.

Akib
  • 193
  • 1
  • 2
  • 13
  • my query is like ` code cricket.findOneAndUpdate( { $and: [ { _id: mongoose.Types.ObjectId('59bb85ad7a64f91145db564e') }, { "player_detail.name": 'abcd' }, ], }, { "player_detail.runs.run": '98', }, function(error, response) { console.log("response", response); } ` here I get a response like `{ MongoError: cannot use the part (player_detail of player_detail.name) to traverse the element.... ` – Akib Sep 21 '17 at 06:33

0 Answers0