addToSet from multiple stages in aggeragate pipeline: invalid operator '$each'

54 views
Skip to first unread message

Mummoorthy M

unread,
Aug 19, 2016, 9:17:47 PM8/19/16
to mongodb-user
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 }

Kevin Adistambha

unread,
Aug 25, 2016, 9:26:12 PM8/25/16
to mongodb-user
Hi

>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.

Could you post:
* your MongoDB version
* some example documents
* how the aggregation result should look like?

Best regards,
Kevin
Reply all
Reply to author
Forward
0 new messages