我正在尝试在MongoDB中建模“游戏”概念,其中“团队”由“玩家”竞争对手。
我有两个集合:players
和 games
。
下面是 games
集合中文档的结构。
{
"_id": { "$oid": "1" },
"teams": [
{
"players": [
{
"player": { "$oid": "2" },
"score": 500,
},
{
"player": { "$oid": "3" },
"score": 550,
}
]
},
{
"players": [
{
"player": { "$oid": "4" },
"score": 500,
},
{
"player": { "$oid": "5" },
"score": 550,
}
]
}
]
}
这是任务:给定一个玩家ID,我想找到此玩家参加的所有比赛。
我尝试了以下方法:
db.games.find( { "teams.players.player._id": "2" } )
然而,这并没有返回任何内容。
顺便说一下,我正在使用以下模式的Mongoose:
playerSchema = Schema
player: { type: Schema.ObjectId, ref: 'Player' }
score: { type: Number }
teamSchema = Schema
players: [ playerSchema ]
gameSchema = Schema
teams: [ teamSchema ]
使用以下 CoffeeScript 查询:
Game.find 'teams.players.player._id': playerId
无论是哪个球员ID,都没有返回任何结果。