Reputation: 19684
Is it possible to add the results of a $sum to an array in grouping?
Something like:
{
"$group" :
{
_id : {ProductId: "$ProductId", Day: "$Day"},
Products : {$push:{clicks: {$sum: "$clicks"}}}
}
}
I would like to store the calculated value of $sum into an array. Can this be done in a grouping?
Upvotes: 3
Views: 2308
Reputation: 26032
Yes you can do it with second group operator.
db.collection.aggregate(
{$group : {_id : {ProductId : "$ProductId", Day : "$Day"},
clicks : {$sum : "$clicks"}}},
{$group : {_id : "$_id", Products : {$push : {clicks : "$clicks"}}}}
)
Upvotes: 4