【发布时间】:2022-01-06 04:35:54
【问题描述】:
我正在 MongoDB 中为基于 NestJs 的网络应用程序保存数据。
我的 MongoDB 数据如下所示
"gameId": "1a2b3c4d5e"
"rounds": [
{
"matches": [
{
"match_id": "1111abc1111",
"team1": {
"team_id": "team8",
"score": 0
},
"team2": {
"team_id": "team2",
"score": 0
}
},
{
"match_id": "2222abc2222",
"team1": {
"team_id": "team6",
"score": 0
},
"team2": {
"team_id": "team5",
"score": 0
}
},
]
}
]
这里我们有每场比赛的gameId,在每场比赛中,有很多回合和很多比赛。每个匹配项都有 match_id。如何获取特定的比赛信息并根据 gameId 和 match_id 对其进行编辑? (注意:我愿意根据 match_id 更新分数)
我尝试过类似的方法
const matchDetails = await this.gameModel.findOne({
gameId: gameId,
rounds: { $elemMatch: { match_id: match_id } },
});
但这不起作用并返回 null。如何正确地做到这一点?
【问题讨论】:
标签: node.js typescript mongodb mongoose nestjs