我有一个名为ad_log
的集合,其中created_at
列为unix时间戳格式。例如created_at: 1469007877
。
如何在laravel jenssegers mongodb库中按年份和月份对记录进行分组。
答案 0 :(得分:1)
假设您的模型是AdLog
试试这个:
AdLog::raw()->agregate([
[ "$group" => [
"_id" => [
"year" => [ "$substr" => [ "$dateStr", 7, 4 ] ],
"month" => [ "$substr" => [ "$dateStr", 4, 2 ] ]
],
"count" => [ "$sum" => 1 ]
]]
]);