I am trying to do a two stage group by to get count distinct, and also a set of distinct values for another field.
I am doing something like this to get count of distinct values of "u1", grouping by "d1".
The first step, groups by "d1, u1", and the second stage groups by "d1" to get distinct count of "u1".
My problem is to get the set of distinct "a1" values from the below query.
======
db.sample.aggregate( [{'$match': {..},
{'$group': {'s': {'$sum': '$s1'}, 'a': {'$addToSet': '$a1'},
'_id': {'d': '$d1', 'u': '$u1'}}},
{'$group': {'s': {'$sum': '$s'}, 'a':
{'$addToSet': '$a' }, '_id': {'d':
'$_id.d'}, 'u': {'$sum': 1}}},
{'$project': {'s': 1, 'a': 1, '_id':
'$_id', 'u': 1}}
========
To get the distinct values of "a1", the first stage creates a set (using addToSet), and the second also does the same.
However, the second addToSet simply appends the whole array into the existing array from the previous stage.
Anyway to get the set of 'a1'? I tried with "each" as follows, but getting an error.
======
db.sample.aggregate( [{'$match': {..},
{'$group': {'s': {'$sum': '$s1'}, 'a': {'$addToSet': '$a1'},
'_id': {'d': '$d1', 'u': '$u1'}}},
{'$group': {'s': {'$sum': '$s'}, 'a':
{'$addToSet': {"$each": '$a' } }, '_id': {'d':
'$_id.d'}, 'u': {'$sum': 1}}},
{'$project': {'s': 1, 'a': 1, '_id':
'$_id', 'u': 1}}
========
{ "ok" : 0, "errmsg" : "invalid operator '$each'", "code" : 15999 }