MongoDb How to aggregate by month and year
You have lost expenseAmount
field during projection stage. Simply add it:
{$project : {
month : {$month : "$entryTime"},
year : {$year : "$entryTime"},
expenseAmount : 1
}},
Note that if field does not exist in document, then $sum
returns 0.
Also note that there is another aggregation operator which performs as you expected - $addFields
. It adds new fields to document and preserves all existing fields from the input document. But in this case you need only expenseAmount field