The $sum accumulator adds numeric values in each $group bucket—the workhorse for revenue totals, quantity rollups, point scores, and the classic $sum: 1 document count.
01
Total per group
Add numeric fields.
02
$sum: 1
Count documents.
03
Expressions OK
Sum computed values.
04
vs $avg
Total vs mean.
05
Null skipped
Non-numbers ignored.
06
Global total
_id: null pattern.
Fundamentals
Definition and Usage
$sum is a MongoDB accumulator used in the $group stage. For each group, it evaluates an expression on every document and returns the arithmetic total of all numeric results.
💡
Beginner tip
Think of $sum as a running plus sign across the group. Sales of 120, 85, and 200 yield 405. Pass the literal 1 instead of a field path to count how many documents landed in each bucket.
Use $sum for order totals, inventory quantities, game points, and KPI rollups. Pair with $avg when you need both total and mean, or with $sum: 1 to show how many records contributed to each total.
$sum is the go-to MongoDB accumulator for additive totals: revenue per store, units shipped, points scored, and document counts via $sum: 1.
Filter with $match first, pair totals with $avg and counts for context, and use expressions when the value to add must be computed. Next: $top for ranked documents per group.
Use $sum: 1 alongside field sums to show count and total together
Place $match before $group to sum only relevant documents
Validate numeric types in source data—strings are silently skipped
Use expressions for line totals: { $sum: { $multiply: […] } }
Sort by the sum field descending for top-N revenue reports
❌ Don’t
Use $sum when you need a mean—use $avg instead
Assume string amounts like "120" are summed—they are ignored
Confuse aggregation $sum with update operator $inc on single documents
Forget that _id: null still outputs _id: null in results
Compare totals across groups without also showing document counts
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $sum
Use these points whenever you total numeric values in aggregation pipelines.
5
Core concepts
💲01
Add values
Total per group.
Basics
🔢02
$sum: 1
Document count.
Pattern
📦03
$group
Accumulator context.
Syntax
⚠04
Skips non-numeric
Strings ignored.
Gotcha
📈05
vs $avg
Total vs mean.
Compare
❓ Frequently Asked Questions
$sum adds numeric values across all documents in each $group bucket. It returns a running total—ideal for revenue, quantities, points, or any additive metric.
Inside $group: { totalAmount: { $sum: "$amount" } }. You can also pass the literal 1—{ count: { $sum: 1 } }—to count documents per group.
It adds 1 for every document in the group, producing a document count. This is the standard pattern for counting rows per bucket alongside other accumulators.
Null, missing fields, strings, and objects are ignored—they do not contribute to the total. If no numeric values exist, $sum returns 0 (unlike $avg which returns null).
$sum returns the total of numeric values. $avg divides that total by the count of numeric values. Use $sum for revenue totals; use $avg for per-unit means.
Yes. Pass any numeric expression: { $sum: { $multiply: ["$price", "$quantity"] } } totals line-item amounts per group.
Did you know?
You can manually compute $avg with $sum on a field and $sum: 1 for count, then divide in $project—but built-in $avg is clearer. The literal $sum: 1 pattern predates the dedicated $count stage and remains very common in tutorials and production pipelines. See the official $sum docs.