title: Count results from mongo aggregate method
body: <p>How to count results from mongo shell aggregate method?</p>
  
  <p>Is there a simpler way than adding </p>
  
  <blockquote>
    <p>{$group:{_id:null,count:{$sum:1}}}</p>
  </blockquote>
  
  <p>to the query?</p>
  
  <p>For example I have following schema:</p>
  
  <pre><code>{
          "_id" : ObjectId("541b2b6813e401118fcf9ec6"),
          "customer" : "Bob",
          "items" : [
                  "pear",
                  "apple"
          ]
  }
  </code></pre>
  
  <p>And I want to count how many pears has Bob ordered (he had multiple orders, and items can contain duplicates). I came with following query:</p>
  
  <pre><code>db.orders.aggregate(
  [
      {
          $match: {
              "customer": {
                  $eq: "Bob"
              }
          }
      },
      {
          "$unwind": "$items"
      },
      {
          $match: {
              "items": {
                  $eq: "pear"
              }
          }
      },
      {
          $group: {
              _id: null,
              count: {
                  $sum: 1
              }
          }
      }
  ]
  )
  </code></pre>
  
  <p>With response:</p>
  
  <pre><code>{ "_id" : null, "count" : 1 }
  </code></pre>
  
  <p>Is there a simpler way?</p>