jenssegers / laravel-mongodb group按年和月分组

时间:2016-07-20 10:43:39

标签: mongodb laravel aggregation-framework

我有一个名为ad_log的集合,其中created_at列为unix时间戳格式。例如created_at: 1469007877。 如何在laravel jenssegers mongodb库中按年份和月份对记录进行分组。

1 个答案:

答案 0 :(得分:1)

假设您的模型是AdLog 试试这个:

AdLog::raw()->agregate([
   [ "$group" => [
       "_id" => [
           "year" => [ "$substr" => [ "$dateStr", 7, 4 ] ],
           "month" => [ "$substr" => [ "$dateStr", 4, 2 ] ]
       ],
       "count" => [ "$sum" => 1 ]
   ]]
]);