Group (By) in Mongoose?

Viewed 21280

I've constructed the query I want in the shell but am having trouble writing it in Mongoose.

db.commentstreams.group({ key: { page_id: true }, reduce: function(obj,prev) { prev.num_comments += obj.num_comments }, initial: { num_comments: 0 } })

I'm a bit confused by the Mongoose syntax; perhaps someone could shed some light on this one. Much thanks.

4 Answers

I worked with similar example, I hope this be helpful

Register.aggregate(
[
  {
   $group: {_id: {campaign: "$campaign"}, quantity: {$sum: 1}}
},
  {
    $limit: 5
  },
  {
    $sort: {date: -1}
  }
], function (err, res) {
        console.log(res)
});
Related