title: Why MongoDB different query plans show different nReturned value?
body: <p>I have a collection <code>faults</code> in my <code>MongoDB</code> database which every document has these fields: <code>rack_name</code>, <code>timestamp</code></p>
  
  <p>Just for sake of testing and comparing performances, I have created these two indexes:</p>
  
  <p><code>rack -&gt; {'rack_name': 1}</code></p>
  
  <p>and </p>
  
  <p><code>time -&gt; {'timestamp': 1}</code></p>
  
  <p>Now I executed the following query with explain():</p>
  
  <pre><code>db.faults.find({
      'rack_name': {
          $in: [ 'providence1', 'helena2' ]
      }, 
      'timestamp': {
          $gt: 1501548359000
      }
  })
  .explain('allPlansExecution') 
  </code></pre>
  
  <p>and here is the result:</p>
  
  <pre><code> {
  "queryPlanner" : {
      "plannerVersion" : 1,
      "namespace" : "quicktester_clone.faults",
      "indexFilterSet" : false,
      "parsedQuery" : {
          "$and" : [ 
              {
                  "timestamp" : {
                      "$gt" : 1501548359000.0
                  }
              }, 
              {
                  "rack_name" : {
                      "$in" : [ 
                          "helena2", 
                          "providence1"
                      ]
                  }
              }
          ]
      },
      "winningPlan" : {
          "stage" : "FETCH",
          "filter" : {
              "timestamp" : {
                  "$gt" : 1501548359000.0
              }
          },
          "inputStage" : {
              "stage" : "IXSCAN",
              "keyPattern" : {
                  "rack_name" : 1
              },
              "indexName" : "rack",
              "isMultiKey" : false,
              "multiKeyPaths" : {
                  "rack_name" : []
              },
              "isUnique" : false,
              "isSparse" : false,
              "isPartial" : false,
              "indexVersion" : 2,
              "direction" : "forward",
              "indexBounds" : {
                  "rack_name" : [ 
                      "[\"helena2\", \"helena2\"]", 
                      "[\"providence1\", \"providence1\"]"
                  ]
              }
          }
      },
      "rejectedPlans" : [ 
          {
              "stage" : "FETCH",
              "filter" : {
                  "rack_name" : {
                      "$in" : [ 
                          "helena2", 
                          "providence1"
                      ]
                  }
              },
              "inputStage" : {
                  "stage" : "IXSCAN",
                  "keyPattern" : {
                      "timestamp" : 1
                  },
                  "indexName" : "time",
                  "isMultiKey" : false,
                  "multiKeyPaths" : {
                      "timestamp" : []
                  },
                  "isUnique" : false,
                  "isSparse" : false,
                  "isPartial" : false,
                  "indexVersion" : 2,
                  "direction" : "forward",
                  "indexBounds" : {
                      "timestamp" : [ 
                          "(1501548359000.0, inf.0]"
                      ]
                  }
              }
          }
      ]
  },
  "executionStats" : {
      "executionSuccess" : true,
      "nReturned" : 43,
      "executionTimeMillis" : 1512,
      "totalKeysExamined" : 221,
      "totalDocsExamined" : 219,
      "executionStages" : {
          "stage" : "FETCH",
          "filter" : {
              "timestamp" : {
                  "$gt" : 1501548359000.0
              }
          },
          "nReturned" : 43,
          "executionTimeMillisEstimate" : 1431,
          "works" : 222,
          "advanced" : 43,
          "needTime" : 177,
          "needYield" : 0,
          "saveState" : 64,
          "restoreState" : 64,
          "isEOF" : 1,
          "invalidates" : 0,
          "docsExamined" : 219,
          "alreadyHasObj" : 0,
          "inputStage" : {
              "stage" : "IXSCAN",
              "nReturned" : 219,
              "executionTimeMillisEstimate" : 71,
              "works" : 221,
              "advanced" : 219,
              "needTime" : 1,
              "needYield" : 0,
              "saveState" : 64,
              "restoreState" : 64,
              "isEOF" : 1,
              "invalidates" : 0,
              "keyPattern" : {
                  "rack_name" : 1
              },
              "indexName" : "rack",
              "isMultiKey" : false,
              "multiKeyPaths" : {
                  "rack_name" : []
              },
              "isUnique" : false,
              "isSparse" : false,
              "isPartial" : false,
              "indexVersion" : 2,
              "direction" : "forward",
              "indexBounds" : {
                  "rack_name" : [ 
                      "[\"helena2\", \"helena2\"]", 
                      "[\"providence1\", \"providence1\"]"
                  ]
              },
              "keysExamined" : 221,
              "seeks" : 2,
              "dupsTested" : 0,
              "dupsDropped" : 0,
              "seenInvalidated" : 0
          }
      },
      "allPlansExecution" : [ 
          {
              "nReturned" : 2,
              "executionTimeMillisEstimate" : 31,
              "totalKeysExamined" : 221,
              "totalDocsExamined" : 221,
              "executionStages" : {
                  "stage" : "FETCH",
                  "filter" : {
                      "rack_name" : {
                          "$in" : [ 
                              "helena2", 
                              "providence1"
                          ]
                      }
                  },
                  "nReturned" : 2,
                  "executionTimeMillisEstimate" : 31,
                  "works" : 221,
                  "advanced" : 2,
                  "needTime" : 219,
                  "needYield" : 0,
                  "saveState" : 64,
                  "restoreState" : 64,
                  "isEOF" : 0,
                  "invalidates" : 0,
                  "docsExamined" : 221,
                  "alreadyHasObj" : 0,
                  "inputStage" : {
                      "stage" : "IXSCAN",
                      "nReturned" : 221,
                      "executionTimeMillisEstimate" : 10,
                      "works" : 221,
                      "advanced" : 221,
                      "needTime" : 0,
                      "needYield" : 0,
                      "saveState" : 64,
                      "restoreState" : 64,
                      "isEOF" : 0,
                      "invalidates" : 0,
                      "keyPattern" : {
                          "timestamp" : 1
                      },
                      "indexName" : "time",
                      "isMultiKey" : false,
                      "multiKeyPaths" : {
                          "timestamp" : []
                      },
                      "isUnique" : false,
                      "isSparse" : false,
                      "isPartial" : false,
                      "indexVersion" : 2,
                      "direction" : "forward",
                      "indexBounds" : {
                          "timestamp" : [ 
                              "(1501548359000.0, inf.0]"
                          ]
                      },
                      "keysExamined" : 221,
                      "seeks" : 1,
                      "dupsTested" : 0,
                      "dupsDropped" : 0,
                      "seenInvalidated" : 0
                  }
              }
          }, 
          {
              "nReturned" : 43,
              "executionTimeMillisEstimate" : 1431,
              "totalKeysExamined" : 221,
              "totalDocsExamined" : 219,
              "executionStages" : {
                  "stage" : "FETCH",
                  "filter" : {
                      "timestamp" : {
                          "$gt" : 1501548359000.0
                      }
                  },
                  "nReturned" : 43,
                  "executionTimeMillisEstimate" : 1431,
                  "works" : 221,
                  "advanced" : 43,
                  "needTime" : 177,
                  "needYield" : 0,
                  "saveState" : 64,
                  "restoreState" : 64,
                  "isEOF" : 1,
                  "invalidates" : 0,
                  "docsExamined" : 219,
                  "alreadyHasObj" : 0,
                  "inputStage" : {
                      "stage" : "IXSCAN",
                      "nReturned" : 219,
                      "executionTimeMillisEstimate" : 71,
                      "works" : 221,
                      "advanced" : 219,
                      "needTime" : 1,
                      "needYield" : 0,
                      "saveState" : 64,
                      "restoreState" : 64,
                      "isEOF" : 1,
                      "invalidates" : 0,
                      "keyPattern" : {
                          "rack_name" : 1
                      },
                      "indexName" : "rack",
                      "isMultiKey" : false,
                      "multiKeyPaths" : {
                          "rack_name" : []
                      },
                      "isUnique" : false,
                      "isSparse" : false,
                      "isPartial" : false,
                      "indexVersion" : 2,
                      "direction" : "forward",
                      "indexBounds" : {
                          "rack_name" : [ 
                              "[\"helena2\", \"helena2\"]", 
                              "[\"providence1\", \"providence1\"]"
                          ]
                      },
                      "keysExamined" : 221,
                      "seeks" : 2,
                      "dupsTested" : 0,
                      "dupsDropped" : 0,
                      "seenInvalidated" : 0
                  }
              }
          }
      ]
  },
  "serverInfo" : {
      "host" : "dtauto-sna01.mascorp.com",
      "port" : 27017,
      "version" : "3.4.4",
      "gitVersion" : "888390515874a9debd1b6c5d36559ca86b44babd"
  },
  "ok" : 1.0
  }
  </code></pre>
  
  <p>Two things I do not understand:</p>
  
  <ol>
  <li><p>When you look at <code>AllPlansExecution</code> the <code>nReturned</code> key has different
  value on each plan. Second plan (index: rack) is actually the winner
  plan and returns 43 results which is the actual return result of the
  whole query, but what are those 2 <code>nReturned</code> result from the first</p></li>
  <li><p>More challenge comes to this point that why the first plan (index:
  time which is the rejected plan) has been reported with less
  <code>executionTimeMillis</code> value 31 than the winner plan
  <code>executionTimeMillis</code> 1431?</p></li>
  </ol>
  
  <p>What's going on?</p>