title: MongoDB - Aggregation Framework (Total Count)
body: <p>When running a normal "find" query on MongoDB I can get the total result count (regardless of limit) by running "count" on the returned cursor. So, even if I limit to result set to 10 (for example) I can still know that the total number of results was 53 (again, for example).</p>
  
  <p>If I understand it correctly, the aggregation framework, however, doesn't return a cursor but simply the results. And so, if I used the <code>$limit</code> pipeline operator, how can I know the total number of results regardless of said limit?</p>
  
  <p>I guess I could run the aggregation twice (once to count the results via <code>$group</code>, and once with <code>$limit</code> for the actual limited results), but this seems inefficient. </p>
  
  <p>An alternative approach could be to attach the total number of results to the documents (via <code>$group</code>) prior to the <code>$limit</code> operation, but this also seems inefficient as this number will be attached to every document (instead of just returned once for the set).</p>
  
  <p>Am I missing something here? Any ideas? Thanks!</p>
  
  <p>For example, if this is the query:</p>
  
  <pre><code>db.article.aggregate(
      { $group : {
          _id : "$author",
          posts : { $sum : 1 }
      }},
      { $sort : { posts: -1 } },
      { $limit : 5 }
  );
  </code></pre>
  
  <p>How would I know how many results are available (before <code>$limit</code>)? The result isn't a cursor, so I can't just run count on it.</p>