我正在尝试按月和按年分组,并返回没有数据的月份。
示例:
如果期间是01/01/2018至01/03/2018,我们只搜索评级和评级仅在2月份,我仍然希望获得1月和3月的评级。
$pipeline = [
['$match'=>[
'rating' => ['$exists' => true],
'date' => [
'$gte' => $da->getStart(true),
'$lte' => $da->getEnd(true)
],
]
],
['$project' => [
'year' => [
'$cond' => [
['$ifNull' => [$date->appendDollarSing(), 0]],
['$year' => $date->appendDollarSing()],
-1
],
'month' => [
'$cond' => [
['$ifNull' => [$date->appendDollarSing(), 0]],
['$month' => $date->appendDollarSing()],
-1
],
'rating' => ['$sum' => '$rating'],
]
],
['$group' => [
'_id' => ['year' => '$year', 'month' =>'$month'],
'rating' => ['$sum' => '$rating'],
'count' => ['$sum' => 1],
]
],
];
以上查询返回评分的月份,因为我使用的评级为true。但是,即使我删除了exists true,它也不会获取没有评级的月份