Mongo Distinct Aggegation

后端 未结 1 1549
广开言路
广开言路 2021-01-24 18:43

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



        
相关标签:
1条回答
  • 2021-01-24 19:23

    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)
            )
    
    0 讨论(0)
提交回复
热议问题