title: MongoDB: How to find by subdocument ID?
body: <p>I am trying to model the concept of <code>games</code> where <code>teams</code> of <code>players</code> compete against each other in MongoDB.</p>
  
  <p>I have two collections: <code>players</code> and <code>games</code>.</p>
  
  <p>This is how a document in <code>games</code> looks like.</p>
  
  <pre><code>{
      "_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,
                  }
              ]
          }
      ]
  }
  </code></pre>
  
  <p>Here's the task: given a player ID I want to find all games in which this player participated.</p>
  
  <p>What I've tried:</p>
  
  <pre><code>db.games.find( { "teams.players.player._id": "2" } )
  </code></pre>
  
  <p>However, this does not return anything.</p>
  
  <p>By the way, I'm using Mongoose with the following schema:</p>
  
  <pre><code>playerSchema = Schema
      player: { type: Schema.ObjectId, ref: 'Player' }
      score: { type: Number }
  
  teamSchema = Schema
      players: [ playerSchema ]
  
  gameSchema = Schema
      teams: [ teamSchema ]
  </code></pre>
  
  <p>with the following CoffeeScript query:</p>
  
  <pre><code>Game.find 'teams.players.player._id': playerId
  </code></pre>
  
  <p>which returns no results for any player ID.</p>