I\'m trying to use the aggregation framework to perform group counts in mongo but the results are not exactly as expected.
Consider the collection bellow
If I'm understanding the question right, what you're trying to get at is
totals of all DISTINCT users_id under a day
Or as I understand it: Count of unique user_ids per day.
For that, you could take the group you already have and cut out the count so that you just have a unique _id.user_id
and _id.day
value:
'$group' => array(
'_id' => array(
'user_id' => '$user_id',
'day' => '$day'
)
)
Then pipe that to another $group
statement that counts the number of documents per day, since there is exactly one for every unique user_id
/day
combination:
'$group' => array(
'_id' => '$_id.day',
'count' => array('$sum' => 1)
)