Created
September 4, 2017 06:15
-
-
Save apriady/895b212e407dd8e9d180b2ede27738d0 to your computer and use it in GitHub Desktop.
Mongoose aggregate to get total amount
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks