title: Gets documents and total count of them in single query include pagination
body: <p>I'm new in mongo and use <strong>mongodb aggregation framework</strong> for my queries. I need to retrieve some records which satisfy certain conditions(<strong>include pagination+sorting)</strong> and also get <strong>total count</strong> of records. </p>
  
  <p>Now, I perform next steps:</p>
  
  <ol>
  <li>Create <code>$match</code> operator<br>
  <code>{ "$match" : { "year" : "2012" , "author.authorName" : { "$regex" :
  "au" , "$options" : "i"}}}</code></li>
  <li>Added sorting and pagination<br>
  <code>{ "$sort" : { "some_field" : -1}} , { "$limit" : 10} , { "$skip" : 0}</code></li>
  </ol>
  
  <p>After querying I receive the expected result: 10 documents with all fields.</p>
  
  <p>For pagination I need to know the total count of records which satisfy these conditions, in my case 25.</p>
  
  <p>I use next query to get count : <code>{ "$match" : { "year" : "2012" , "author.authorName" : { "$regex" : "au" , "$options" : "i"}}} , { "$group" : { "_id" : "$all" , "reviewsCount" : { "$sum" : 1}}} ,  { "$sort" : { "some_field" : -1}} , { "$limit" : 10} , { "$skip" : 0}</code> </p>
  
  <p>But I don't want to perform <strong>two separate queries</strong>: one for retrieving documents and second for total counts of records which satisfy certain conditions.</p>
  
  <p>I want do it in one <strong>single query</strong> and get result in next format:</p>
  
  <pre><code>{
          "result" : [
                  {
              "my_documets": [
                          {
                          "_id" : ObjectId("512f1f47a411dc06281d98c0"),
                          "author" : {
                                  "authorName" : "author name1",
                                  "email" : "email1@email.com"
                              }
                          },
                          {
                          "_id" : ObjectId("512f1f47a411dc06281d98c0"),
                          "author" : {
                                  "authorName" : "author name2",
                                  "email" : "email2@email.com"
                              }
                          }, .......
  
                      ],
                      "total" : 25
                  }
          ],
          "ok" : 1
  }
  </code></pre>
  
  <p>I tried modify the group operator :   <code>{ "$group" : { "_id" : "$all" , "author" : "$author"  "reviewsCount" : { "$sum" : 1}}}</code> 
  But in this case I got : "<strong>exception: the group aggregate field 'author' must be defined as an expression inside an object</strong>". If add all fields in _id  then reviewsCount always = 1 because all records are different.</p>
  
  <p>Nobody know how it can be implement in single query ? Maybe mongodb has some features or operators for this case? Implementation with using two separate query reduces performance for querying thousand or millions records. In my application it's very critical performance  issue.</p>
  
  <p>I've been working on this all day and haven't been able to find a solution, so thought i'd turn to the stackoverflow community.</p>
  
  <p>Thanks.</p>