Add static field to grouped data in Mongodb -
how add static field result using group operation in mongodb. query looks like:
db.sales.aggregate({ $group : { _id: { year: { $year: '$date' }, }, amount: { $sum: 1 } } });
than result:
{ "result" : [ { "_id" : { "year" : 2013 }, "amount" : 43433 }, ... ] "ok" : 1 }
i need add field called type
value 'year'
in each object of result.
i need add project operation literal 'year'. query looks like:
db.sales.aggregate([ { $group : { _id: { year: { $year: '$registrationdate' } }, amount: { $sum: 1 } }, }, { $project: { type: { $literal: 'year' }, } } ]);
Comments
Post a Comment