I'm trying to match by a given year (lets say 2008) and from there, group by months in order to find the count for each month in 2008. So far, im able to group by month, but im unable to filter by only a particular year. The date
column is stored as a datetime object
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
},
}])
CodePudding user response:
{$match: {$expr: {$eq: [2008, {$year: "$date"}] } } }
$expr
allows you to use any valid aggregation expression within a $match