title: MongoDB $group + $sum aggregation is very slow
body: <p>I have an aggregation query in MongoDB:</p>
  <pre><code>[{
      $group: {
          _id: '$status',
          status: {
              $sum: 1
          }
      }
  }]
  </code></pre>
  <p>It is running on a collection that has ~80 million documents. The status field is indexed, yet the query is very slow and runs for around 60 seconds or more.</p>
  <p>I did an <code>explain()</code> on the query, but still got almost nowhere:</p>
  <pre><code>{
          &quot;explainVersion&quot; : &quot;1&quot;,
          &quot;stages&quot; : [
                  {
                          &quot;$cursor&quot; : {
                                  &quot;queryPlanner&quot; : {
                                          &quot;namespace&quot; : &quot;loa.document&quot;,
                                          &quot;indexFilterSet&quot; : false,
                                          &quot;parsedQuery&quot; : {
  
                                          },
                                          &quot;queryHash&quot; : &quot;B9878693&quot;,
                                          &quot;planCacheKey&quot; : &quot;8EAA28C6&quot;,
                                          &quot;maxIndexedOrSolutionsReached&quot; : false,
                                          &quot;maxIndexedAndSolutionsReached&quot; : false,
                                          &quot;maxScansToExplodeReached&quot; : false,
                                          &quot;winningPlan&quot; : {
                                                  &quot;stage&quot; : &quot;PROJECTION_SIMPLE&quot;,
                                                  &quot;transformBy&quot; : {
                                                          &quot;status&quot; : 1,
                                                          &quot;_id&quot; : 0
                                                  },
                                                  &quot;inputStage&quot; : {
                                                          &quot;stage&quot; : &quot;COLLSCAN&quot;,
                                                          &quot;direction&quot; : &quot;forward&quot;
                                                  }
                                          },
                                          &quot;rejectedPlans&quot; : [ ]
                                  }
                          }
                  },
                  {
                          &quot;$group&quot; : {
                                  &quot;_id&quot; : &quot;$status&quot;,
                                  &quot;status&quot; : {
                                          &quot;$sum&quot; : {
                                                  &quot;$const&quot; : 1
                                          }
                                  }
                          }
                  }
          ],
          &quot;serverInfo&quot; : {
                  &quot;host&quot; : &quot;rack-compute-2&quot;,
                  &quot;port&quot; : 27017,
                  &quot;version&quot; : &quot;5.0.6&quot;,
                  &quot;gitVersion&quot; : &quot;212a8dbb47f07427dae194a9c75baec1d81d9259&quot;
          },
          &quot;serverParameters&quot; : {
                  &quot;internalQueryFacetBufferSizeBytes&quot; : 104857600,
                  &quot;internalQueryFacetMaxOutputDocSizeBytes&quot; : 104857600,
                  &quot;internalLookupStageIntermediateDocumentMaxSizeBytes&quot; : 104857600,
                  &quot;internalDocumentSourceGroupMaxMemoryBytes&quot; : 104857600,
                  &quot;internalQueryMaxBlockingSortMemoryUsageBytes&quot; : 104857600,
                  &quot;internalQueryProhibitBlockingMergeOnMongoS&quot; : 0,
                  &quot;internalQueryMaxAddToSetBytes&quot; : 104857600,
                  &quot;internalDocumentSourceSetWindowFieldsMaxMemoryBytes&quot; : 104857600
          },
          &quot;command&quot; : {
                  &quot;aggregate&quot; : &quot;document&quot;,
                  &quot;pipeline&quot; : [
                          {
                                  &quot;$group&quot; : {
                                          &quot;_id&quot; : &quot;$status&quot;,
                                          &quot;status&quot; : {
                                                  &quot;$sum&quot; : 1
                                          }
                                  }
                          }
                  ],
                  &quot;explain&quot; : true,
                  &quot;cursor&quot; : {
  
                  },
                  &quot;lsid&quot; : {
                          &quot;id&quot; : UUID(&quot;a07e17fe-65ff-4d38-966f-7517b7a5d3f2&quot;)
                  },
                  &quot;$db&quot; : &quot;loa&quot;
          },
          &quot;ok&quot; : 1
  }
  </code></pre>
  <p>I see that it does a full <code>COLLSCAN</code>, I just can't understand why.</p>
  <p>I plan on supporting a couple hundred million (or even a billion) documents in that collection, but this problem hijacks my plans for seemingly no reason.</p>