title: Obtaining $group result with group count
body: <p>Assuming I have a collection called "posts" (in reality it is a more complex collection, posts is too simple) with the following structure:</p>
  
  <pre><code>&gt; db.posts.find()
  
  { "_id" : ObjectId("50ad8d451d41c8fc58000003"), "title" : "Lorem ipsum", "author" : 
  "John Doe", "content" : "This is the content", "tags" : [ "SOME", "RANDOM", "TAGS" ] }
  </code></pre>
  
  <p>I expect this collection to span hundreds of thousands, perhaps millions, that I need to query for posts by tags and group the results by tag and display the results paginated. This is where the aggregation framework comes in. I plan to use the aggregate() method to query the collection:</p>
  
  <pre><code>db.posts.aggregate([
    { "$unwind" : "$tags" },
    { "$group" : {
        _id: { tag: "$tags" },
        count: { $sum: 1 }
    } }
  ]);
  </code></pre>
  
  <p>The catch is that to create the paginator I would need to know the length of the output array. I know that to do that you can do:</p>
  
  <pre><code>db.posts.aggregate([
    { "$unwind" : "$tags" },
    { "$group" : {
        _id: { tag: "$tags" },
        count: { $sum: 1 }
    } }
    { "$group" : {
        _id: null,
        total: { $sum: 1 }
    } }
  ]);
  </code></pre>
  
  <p>But that would discard the output from previous pipeline (the first group). Is there a way that the two operations be combined while preserving each pipeline's output? I know that the output of the whole aggregate operation can be cast to an array in some language and have the contents counted but there may be a possibility that the pipeline output may exceed the 16Mb limit. Also, performing the same query just to obtain the count seems like a waste.</p>
  
  <p>So is obtaining the document result and count at the same time possible? Any help is appreciated.</p>