5

I have a MongoDB aggregation operation that uses a couple of groups:

{ $group: { "_id": { "vid": "$visitor_id", "pid":"$project.id" } } }
{ $group: { "_id": "$_id.vid", "count": { $sum: 1} } }

that returns the following data:

{
  "results": [
    {
      "_id": "user1",
      "count": 1
    }, 
    {
      "_id": "user2",
      "count": 2
    },
    {
      "_id": "user3",
      "count": 1
    }
  ]
}

How could I go about returning the total number of users with more than 1 project (count field). In this case it would be something like:

{
  total: 1
}

because only user2 has more than 1 project (count > 1).

I've tried adding the following group operation to no avail:

{ 
  $group: { 
    "_id": null, 
    count: {
      $sum: {$cond: [{$gt: ['$_id.count', 1]}, 1, 0]}
    } 
  } 
}

Any ideas?

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
ashe540
  • 2,041
  • 2
  • 17
  • 17
  • 1
    Can't you just have a final aggregation pipeline step as the **`$match`** operator to filter out the counts i.e. `{ "$match": {"count": {"$gt": 1}}}`? – chridam Mar 01 '16 at 15:43

1 Answers1

3

You can achieve your desired result as simple as adding the following three aggregation stages into your pipeline:

{$match: {count: {$gt: 1}}}
{$group: {_id: null, total: {$sum: 1}}}
{$project: {_id: 0, total: 1}}
  1. As chridam mentined you can have a $match stage to filter all the users that have more than 1 projects.
  2. Then use another $group stage to count the total number of such users.
  3. The third step is just cosmetics, leaves only the total field.
Community
  • 1
  • 1
bagrat
  • 7,158
  • 6
  • 29
  • 47
  • 1
    Absolutely right. I don't know why I didn't think of that. Thanks a lot to both you and @chridam for the help! – ashe540 Mar 01 '16 at 15:58
  • That's because the `$group` stage is such a cool thing you want to put all into it :) Glad it worked! – bagrat Mar 01 '16 at 16:10