MongoDB,在嵌套的文档数组上尝试$pull时,无法使用(...)的(...)部分遍历元素

cl25kdpy  于 2022-12-12  发布在  Go
关注(0)|答案(1)|浏览(160)

I am trying to $pull a certain document contained within a nested array, within another nested array.
The structure goes as follows:

*clubs{ "club_id",
       "players"[{"player_id": 123,
                 "comments": [{"_id": "id"}, 
                              {"_id": "id2"}]
                                             }]
      }

I am trying to use the below in mongosh:

db.clubs.updateOne({"players.player_id": 363205}, {$pull: {"players.comments": {"players.comments._id": "5c61f001-768c-11ed-892f-346f24b28bd0"}}})

But when I submit this I get the following error:

MongoServerError: Cannot use the part (comments) of (players.comments) to traverse the element ({players: ...

Can someone please lend a hand here and let me know where I am going wrong? I've had a similar query work earlier, however it was only on 1 nested array of documents, rather than a nested array in a document that itself is contained in a nested array. Thanks in advance! :)

wbgh16ku

wbgh16ku1#

使用$[<identifier>] filtered positional operator更新。

db.clubs.updateOne({
  "players.player_id": 363205
},
{
  $pull: {
    "players.$[player].comments": {
      "_id": "5c61f001-768c-11ed-892f-346f24b28bd0"
    }
  }
},
{
  arrayFilters: [
    {
      "player.player_id": 363205
    }
  ]
})

Demo @ Mongo Playground

相关问题