title: MongoDB seems to choose the wrong index when doing aggregate
body: <p>A testing mongodb(version 3.0.1) running on Amazon EC2(3.14.33-26.47.amzn1.x86_64, t2.medium: 2 vcpus, 4G mem).</p>
  
  <p>And a collection "access_log"(about 40,000,000 records, 1,000,000 each day), and some indexes on it:</p>
  
  <pre><code>...
  
  db.access_log.ensureIndex({ visit_dt: 1, 'username': 1 })
  
  db.access_log.ensureIndex({ visit_dt: 1, 'file': 1 })
  ...
  </code></pre>
  
  <p>When doing following "aggregate", it's extremely slow(takes several hours):</p>
  
  <pre><code>db.access_log.aggregate([
      { "$match": { "visit_dt": { "$gte": ISODate('2015-03-09'), "$lt": ISODate('2015-03-11') } } },
      { "$project": { "file": 1,  "_id": 0 } },
      { "$group": { "_id": "$file", "count": { "$sum": 1 } } },
      { "$sort": { "count": -1 } }
  ])
  </code></pre>
  
  <p>All fields <strong>needed</strong> for this aggregation are <strong>included</strong> in the 2nd index ({ visit_dt: 1, 'file': 1 }, that is "visit_dt_1_file_1").</p>
  
  <p>So I am very confused that why mongodb does not use this index, but the other one.</p>
  
  <p>When explaining plan, always get following information, which I do not understand at all.</p>
  
  <p>Could you please help? Thanks a lot!</p>
  
  <pre><code>&gt; db.access_log.aggregate([
  ...     { "$match": { "visit_dt": { "$gte": ISODate('2015-03-09'), "$lt": ISODate('2015-03-11') } } },
  ...     { "$project": { "file": 1,  "_id": 0 } },
  ...     { "$group": { "_id": "$file", "count": { "$sum": 1 } } },
  ...     { "$sort": { "count": -1 } }
  ... ], { explain: true } );
  {
          "stages" : [
                  {
                          "$cursor" : {
                                  "query" : {
                                          "visit_dt" : {
                                                  "$gte" : ISODate("2015-03-09T00:00:00Z"),
                                                  "$lt" : ISODate("2015-03-11T00:00:00Z")
                                          }
                                  },
                                  "fields" : {
                                          "file" : 1,
                                          "_id" : 0
                                  },
                                  "queryPlanner" : {
                                          "plannerVersion" : 1,
                                          "namespace" : "xxxx.access_log",
                                          "indexFilterSet" : false,
                                          "parsedQuery" : {
                                                  "$and" : [
                                                          {
                                                                  "visit_dt" : {
                                                                          "$lt" : ISODate("2015-03-11T00:00:00Z")
                                                                  }
                                                          },
                                                          {
                                                                  "visit_dt" : {
                                                                          "$gte" : ISODate("2015-03-09T00:00:00Z")
                                                                  }
                                                          }
                                                  ]
                                          },
                                          "winningPlan" : {
                                                  "stage" : "FETCH",
                                                  "inputStage" : {
                                                          "stage" : "IXSCAN",
                                                          "keyPattern" : {
                                                                  "visit_dt" : 1,
                                                                  "username" : 1
                                                          },
                                                          "indexName" : "visit_dt_1_username_1",
                                                          "isMultiKey" : false,
                                                          "direction" : "forward",
                                                          "indexBounds" : {
                                                                  "visit_dt" : [
                                                                          "[new Date(1425859200000), new Date(1426032000000))"
                                                                  ],
                                                                  "username" : [
                                                                          "[MinKey, MaxKey]"
                                                                  ]
                                                          }
                                                  }
                                          },
                                          "rejectedPlans" : [
    ...
                                                  {
                                                          "stage" : "FETCH",
                                                          "inputStage" : {
                                                                  "stage" : "IXSCAN",
                                                                  "keyPattern" : {
                                                                          "visit_dt" : 1,
                                                                          "file" : 1
                                                                  },
                                                                  "indexName" : "visit_dt_1_file_1",
                                                                  "isMultiKey" : false,
                                                                  "direction" : "forward",
                                                                  "indexBounds" : {
                                                                          "visit_dt" : [
                                                                                  "[new Date(1425859200000), new Date(1426032000000))"
                                                                          ],
                                                                          "file" : [
                                                                                  "[MinKey, MaxKey]"
                                                                          ]
                                                                  }
                                                          }
                                                  },
  ...
                                          ]
                                  }
                          }
                  },
                  {
                          "$project" : {
                                  "_id" : false,
                                  "file" : true
                          }
                  },
                  {
                          "$group" : {
                                  "_id" : "$file",
                                  "count" : {
                                          "$sum" : {
                                                  "$const" : 1
                                          }
                                  }
                          }
                  },
                  {
                          "$sort" : {
                                  "sortKey" : {
                                          "count" : -1
                                  }
                          }
                  }
          ],
          "ok" : 1
  }
  </code></pre>