Nick
Nick

Reputation: 19684

MongoDb Aggregation Framework - Can $sum result be used in $push?

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

Answers (1)

Parvin Gasimzade
Parvin Gasimzade

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

Related Questions