Skip to content

Instantly share code, notes, and snippets.

@apriady
Created September 4, 2017 06:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apriady/895b212e407dd8e9d180b2ede27738d0 to your computer and use it in GitHub Desktop.
Save apriady/895b212e407dd8e9d180b2ede27738d0 to your computer and use it in GitHub Desktop.
Mongoose aggregate to get total amount
var mongoose = require('mongoose');
app.service('donation').Model
.aggregate([
{
$match: {
eventid: new mongoose.Types.ObjectId(req.body.event)
}
},
{
$group: {
_id: '$eventid', //$region is the column name in collection
total: {$sum: '$amount'},
}
}
], function (err, result) {
if (err) {
console.log(err);
} else {
res.json('$'+number_format((result[0].total/100),2));
}
});
@h4n2k
Copy link

h4n2k commented Sep 4, 2017

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment