title: Using MongoDB's positional operator $ in a deeply nested document query
body: <p>Is it possible to use positional operator '$' in combination with a query on a deeply-nested document array?</p>
  
  <p>Consider the following nested document defining a 'user':</p>
  
  <pre><code>{
     username: 'test',
     kingdoms: [
  
        {
            buildings: [
  
                {
                    type: 'castle'
  
                },
                {
                    type: 'treasury'
                },
  
                ...
  
            ]
  
        },
  
        ...
  
     ] 
  }
  </code></pre>
  
  <p>We'd like to return the 'castles' for a particular user e.g. in a form:</p>
  
  <pre><code>{
      kingdoms: [{
  
          buildings: [{
  
              type: 'castle'
  
          }]
  
      }]
  }
  </code></pre>
  
  <p>Because you cannot use the $ operator twice (<a href="https://jira.mongodb.org/browse/server-831" rel="nofollow noreferrer">https://jira.mongodb.org/browse/server-831</a>) I know that I can't also query for a particular kingdom, so I'm trying to write a find statement for the nth kingdom.</p>
  
  <p>This seems to make sense when updating a deeply-nested sub-document (<a href="https://stackoverflow.com/questions/18173482/mongodb-update-deeply-nested-subdocument">Mongodb update deeply nested subdocument</a>) but I'm having less success with the find query.</p>
  
  <p>I can return the first kingdom's buildings with the query:</p>
  
  <pre><code>db.users.findOne(
      { username: 'test' },
      { kingdoms: {$slice: [0, 1]}, 'kingdom.buildings': 1 }
  );
  </code></pre>
  
  <p>But this returns <strong>all</strong> the buildings of that kingdom.</p>
  
  <p>Following the single-level examples of position operator I'm trying a query like this:</p>
  
  <pre><code>db.users.findOne(
      { username: 'test', 'kingdoms.buildings.type': 'castle' },
      { kingdoms: {$slice: [n, 1]}, 'kingdom.buildings.$': 1 }
  );
  </code></pre>
  
  <p>so as to be in the form:</p>
  
  <pre><code>db.collection.find( { &lt;array.field&gt;: &lt;value&gt; ...}, { "&lt;array&gt;.$": 1 } ) 
  </code></pre>
  
  <p>as described in the documentation <a href="http://docs.mongodb.org/manual/reference/operator/projection/positional/#proj._S_" rel="nofollow noreferrer">http://docs.mongodb.org/manual/reference/operator/projection/positional/#proj.<em>S</em></a></p>
  
  <p>However this fails with the error:</p>
  
  <pre><code>Positional operator does not match the query specifier
  </code></pre>
  
  <p>Presumably because kingdoms.buildings isn't considered an array. I've also tried <strong>kingdoms.0.buildings</strong></p>
  
  <p>It is confusing because this appears to work for updates (according to <a href="https://stackoverflow.com/questions/18173482/mongodb-update-deeply-nested-subdocument">Mongodb update deeply nested subdocument</a>)</p>
  
  <p>Have I just got the syntax wrong or is this not supported? If so is there a way to achieve something similar?</p>