title: How to count number of groups that meet a certain condition with MongoDB aggregate framework?
body: <p>I have a MongoDB aggregation operation that uses a couple of groups:</p>
  
  <pre><code>{ $group: { "_id": { "vid": "$visitor_id", "pid":"$project.id" } } }
  { $group: { "_id": "$_id.vid", "count": { $sum: 1} } }
  </code></pre>
  
  <p>that returns the following data:</p>
  
  <pre><code>{
    "results": [
      {
        "_id": "user1",
        "count": 1
      }, 
      {
        "_id": "user2",
        "count": 2
      },
      {
        "_id": "user3",
        "count": 1
      }
    ]
  }
  </code></pre>
  
  <p>How could I go about returning the total number of users with more than 1 project (<code>count</code> field). In this case it would be something like:</p>
  
  <pre><code>{
    total: 1
  }
  </code></pre>
  
  <p>because only <code>user2</code> has more than 1 project (<code>count</code> > 1).</p>
  
  <p>I've tried adding the following group operation to no avail:</p>
  
  <pre><code>{ 
    $group: { 
      "_id": null, 
      count: {
        $sum: {$cond: [{$gt: ['$_id.count', 1]}, 1, 0]}
      } 
    } 
  }
  </code></pre>
  
  <p>Any ideas?</p>