我想更新两个嵌套数组mongoDB对象

时间:2017-09-21 05:25:12

标签: mongodb

我目前正在开发MongoDB。有一个条件我无法处理它。所以我需要你的帮助。

我的收藏数据就像,

{
  "_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"
        }
      ]
    }
  ]
}

现在我想更新播放器运行,这里条件就像,

  1. 收藏ID为59bb85ad7a64f91145db564e
  2. 玩家名称为abcd
  3. 1'st
  4. 此处集合名称为cricket。我正在尝试使用像

    这样的查询来更新它
    cricket.findOneAndUpdate(
    {
      $and: [
        { _id: mongoose.Types.ObjectId('59bb85ad7a64f91145db564e') },
        { "player_detail.name": 'abcd' }, 
      ],
    },
    {
      "player_detail.runs.run": '98',
    },
    function(error, response) {
      console.log("response", response);
    }
    

    在这里,我收到了{ MongoError: cannot use the part (player_detail of player_detail.name) to traverse the element....

    之类的回复

    所以请帮助我如何更新满足以上三个条件的玩家run。 提前谢谢。

0 个答案:

没有答案