我創建了一個MongoDB聚合查詢,該查詢將給我當前月份的數據總和,但是如何修改此數據它會從每個月的每一天返回一個數組,每天的總數(我認爲這是可能的,但我發現很難得到它的工作)?如果這是不可能的,是否有比使用循環和運行30組查詢更好的方法?使用一個MongoDB組()查詢爲該月的每一天創建一個總和數組
我正在使用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'))
)
)
)
);