0PricingLogin
MongoDB Academy · Lesson

$sum, $avg, $min, $max: Numeric Aggregation

Learners will compute totals, averages, and extremes within $group and also use these accumulators as expression operators in $project.

Numeric Accumulators in $group

MongoDB's $group stage uses accumulators to compute aggregate values from grouped documents. The most fundamental numeric accumulators are $sum, $avg, $min, and $max. Each accumulator processes all documents in a group and produces a single output value. These operators form the backbone of analytics pipelines.

Using $sum to Count and Total

The $sum accumulator has two common uses: counting documents by passing a literal value like 1, and summing a field by referencing a numeric field. When a field is missing or null, $sum treats it as zero. This makes it safe to use on optional numeric fields without extra null-checks.

db.orders.aggregate([
  {
    $group: {
      _id: '$status',
      orderCount: { $sum: 1 },
      totalRevenue: { $sum: '$amount' }
    }
  }
])

All lessons in this course

  1. $sum, $avg, $min, $max: Numeric Aggregation
  2. $push and $addToSet: Building Arrays in Groups
  3. $first, $last, and $top/$bottom Accumulators
  4. Window Functions With $setWindowFields
← Back to MongoDB Academy