I am trying to simulate a concept gameswhere teamsof playerscompete with each other in MongoDB.
I have two collections: playersand games.
Here's what the document looks like in 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,
}
]
}
]
}
Here's the problem: given the ID of the player, I want to find all the games in which this player participated.
What I tried:
db.games.find( { "teams.players.player._id": "2" } )
However, this does not return anything.
By the way, I am using Mongoose with the following scheme:
playerSchema = Schema
player: { type: Schema.ObjectId, ref: 'Player' }
score: { type: Number }
teamSchema = Schema
players: [ playerSchema ]
gameSchema = Schema
teams: [ teamSchema ]
with the following CoffeeScript request:
Game.find 'teams.players.player._id': playerId
which does not return results for any player identifier.