我创建了一个 MongoDB 聚合查询,它将为我提供当月的数据总和,但是我如何修改它以便它从当月的每一天返回一个数组,以及每一天的总数(我假设这是可能的,但我发现很难让它工作)?如果这是不可能的,有没有比使用循环和运行 30 个组查询更好的方法呢?
我正在使用 PHP 驱动程序,但 shell 中的答案同样有用。
$total_this_month = $db->test->group(
array( ),
array(
'sum' => 0
),
new MongoCode( 'function(doc, out){ out.sum += doc.data; }' ),
array(
'condition' => array(
'time' => array(
'$gte' => new MongoDate(strtotime('first day of this month, 00:00:00')),
'$lte' => new MongoDate(strtotime('last day of this month, 23:59:59'))
)
)
)
);