Skip to content

Instantly share code, notes, and snippets.

@arahmanali
Last active June 19, 2022 10:08
Show Gist options
  • Save arahmanali/ee7e4a82938212b2a512460e64f29d86 to your computer and use it in GitHub Desktop.
Save arahmanali/ee7e4a82938212b2a512460e64f29d86 to your computer and use it in GitHub Desktop.
Mongoose sum and group model's field.
/* fetch all users user_totaldocs and user_totalthings values and want to sum those variables.*/
var userSchema = mongoose.Schema({
local : {
...
...
user_id : String,
user_totaldocs : Number,
user_totalthings : Number
....
}
});
var User = mongoose.model('User', userSchema);
/*For example, to calculate sums (per user document) you can use the $add expression in a $project stage:*/
User.aggregate(
// Limit to relevant documents and potentially take advantage of an index
{ $match: {
user_id: "foo"
}},
{ $project: {
user_id: 1,
total: { $add: ["$user_totaldocs", "$user_totalthings"] }
}}
)
/* To calculate totals across multiple documents you need to use a $group stage with a $sum accumulator, for example: */
User.aggregate(
{ $group: {
_id: null,
total: { $sum: { $add: ["$user_totaldocs", "$user_totalthings"] } },
totaldocs: { $sum: "$user_totaldocs" },
totalthings: { $sum: "$user_totalthings" }
}}
)
/*
A group _id of null will combine values from all documents passed to the $group stage,
but you can also use other criteria here (such as grouping by user_id).
i.e. _id: user_id
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment