title: MongoDB: covered query with index on subdocuments
body: <p><strong>Input</strong></p>
  
  <p>I have a collection of documents like these:</p>
  
  <pre><code>{_id: {d: 1, t: 1}, ...}
  {_id: {d: 1, t: 2}, ...}
  {_id: {d: 2, t: 1}, ...}
  ...
  </code></pre>
  
  <p><strong>Problem</strong></p>
  
  <p>I'm trying to select all <code>_id</code> values with <code>_id.d = 2</code> with following query:</p>
  
  <pre><code>db.coll.find({_id: {$gte: {d: 2}, $lt: {d: 3}}}, {_id: 1})
  </code></pre>
  
  <p>(Note that I'm not referring <code>_id.d</code> directly in the query)</p>
  
  <p>But this query doesn't seem to be covered since we have IXSCAN as a descendant of FETCH:</p>
  
  <pre><code>"inputStage" : {
      "stage" : "FETCH",
      "filter" : {
              "$and" : [
                      {
                              "_id" : {
                                      "$lt" : {
                                              "d" : 1210
                                      }
                              }
                      },
                      {
                              "_id" : {
                                      "$gte" : {
                                              "d" : 1209
                                      }
                              }
                      }
              ]
      },
      "inputStage" : {
              "stage" : "IXSCAN",
              "keyPattern" : {
                      "_id" : 1
              },
              "indexName" : "_id_",
              "isMultiKey" : false,
              "direction" : "forward",
              "indexBounds" : {
                      "_id" : [
                              "[{ d: 1209 }, { d: 1210 })"
                      ]
              }
      }
  }
  </code></pre>
  
  <p>One of <a href="https://docs.mongodb.com/v3.0/core/query-optimization/#restrictions-on-indexed-fields" rel="nofollow">documented restrictions</a> of covered queries is:</p>
  
  <blockquote>
    <p>• any of the indexed fields in the query predicate or returned in the projection are fields in embedded documents.</p>
  </blockquote>
  
  <p>But my query doesn't seem to refer any fields in embedded documents, it only states index bounds on embedded documents as a whole.</p>
  
  <p><strong>Questions</strong></p>
  
  <p>Does my query falls under quoted restriction? Is it possible to have a covered query in my case? Why does MongoDB need to do FETCH to fulfill this query?</p>