【发布时间】:2021-12-30 09:30:17
【问题描述】:
我正在尝试按给定年份(比如说 2008 年)进行匹配,然后从那里按月分组,以便找到 2008 年每个月的计数。到目前为止,我能够按月分组,但我无法仅按特定年份过滤。 date 列存储为日期时间对象
database.coll.aggregate([
# some expression that will only select year e.g. 2018
{$match : {}},
{$group : {
_"id" : {$month : $date}, # group by year
"total" : {$sum : 1} # count values in each group
},
}])
【问题讨论】:
-
{$match: {$expr: {$eq: [2008, {$year: "$date"}] } } }