MongoDB group by hour

后端 未结 3 420
慢半拍i
慢半拍i 2020-12-03 18:25

I save tweets to mongo DB:

 twit.stream(\'statuses/filter\', {\'track\': [\'animal\']}, function(stream) {
    stream.on(\'data\', function(data) {
        c         


        
3条回答
  •  误落风尘
    2020-12-03 19:11

    There should be no need to use a $project stage here as the date operator functions can just be employed directly in the $group stage when defining the grouping _id. This saves having to process the entire collection in order to get the result:

    Also you are just counting, so simply { "$sum" : 1 }, in which defining a field that didn't exist was the problem resulting in 0.

        $this->collection->aggregate(array(
            array(
                '$group' => array(
                    "_id" => array( 
                        "y" => array( '$year' => '$created_at' ),
                        "m" => array( '$month' => '$created_at' ),
                        "d" => array( '$dayOfMonth' => '$created_at' ),
                        "h" => array( '$hour' => '$created_at' ),
                    ),
                    "total" => array( '$sum' => 1 ),
                ),
            )
        ));
    

    If anything, add a $match stage at the start of the pipeline in order to filter the date. If one day is acceptable for output then you only need to define the $hour in the grouping and you are reducing the working set size, which means faster. And probably what you want to do anyway.

提交回复
热议问题