2017-03-02 57 views
0

我試圖按日期分組所有條目,並將NULL分組到自己的組,但我沒有得到正確的語法。按月份和年份組,包括NULL的

我已經試過

db.DateCollection.aggregate({$project:{year:{$ifNull:[$year:"$received-date","NULL"]},month:{$ifNull:[$month:"$received-date","NULL"]}}},{$group:{_id:{year:"$year",month:"$month"},count:{$sum:1}}}); 

這使「失蹤]之後元素列表」我因爲$ IFNULL內$一年的猜測。

也試過

db.DateCollection.aggregate([{$group:{_id:{$month:"$received-date"},count:{$sum:1}}}]); 

這給「不能從BSON類型NULL轉換爲Date」猜測了,因爲它不能NULL轉換爲一個日期。

+0

這些NULL實際上是NULL還是它們是不存在的字段? – lascort

+0

順便說一下,你用的是哪個版本的mongo? – lascort

+0

有些是空的,有些是不存在的。 – f1wade

回答

0

您可以使用缺省日期條目的缺省日期值。下面的例子我已經使用紀元(1970-01-01)作爲默認值。

​​