The $max accumulator finds the highest value in each $group bucket—ideal for top scores, peak prices, highest quantities, and range reports alongside $min.
01
Peak per group
Largest value in the bucket.
02
Simple syntax
{ $max: "$field" }
03
No $sort needed
Scans all docs in group.
04
Expressions OK
Max of computed values.
05
vs $last
Peak value vs last doc.
06
Null handling
Skips null when others exist.
Fundamentals
Definition and Usage
$max is a MongoDB accumulator used in the $group stage. For each group, it evaluates an expression on every document and returns the maximum value found.
💡
Beginner tip
Unlike $first or $last, $max does not care about document order—it compares values across the whole group. Scores 85, 92, and 78 yield 92 whether or not you sorted first.
Use $max for leaderboard peaks, inventory high-water marks, price ceilings, and validation checks. Pair with $min for min/max ranges, or use $topN when you need the full document that achieved the peak, not just the number.
Use $max + $min in one $group for score/price ranges
Filter with $match before $group to max only relevant documents
Use expressions when the peak of a computed value matters (line totals, durations)
Validate numeric types with $match: { field: { $type: "number" } } when data is messy
Use $top or $bottom when you need identifying fields from the peak record
❌ Don’t
Confuse $max with $last (peak vs last document’s value)
Assume $max returns the student name—it returns the maximum value only
Compare mixed types without understanding BSON type ordering
Expect $max to traverse into array elements inside $group
Use $max when you need N largest values—use $maxN instead
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $max
Use these points when finding peak values inside $group.
5
Core concepts
📈01
Peak value
Highest in the group.
Basics
📝02
Simple syntax
{ $max: "$field" }
Syntax
📊03
No $sort
Scans whole group.
Pattern
🔄04
vs $last
Peak vs trailing doc.
Compare
💲05
+ $min
Min/max ranges.
Pair
❓ Frequently Asked Questions
$max returns the highest value of an expression across all documents in each $group bucket. It scans every document in the group and picks the maximum—no $sort required.
Inside $group: { maxScore: { $max: "$score" } }. It also works as an expression in $project, $addFields, and window stages—but this tutorial focuses on the $group accumulator.
Grouping with _id: null puts all documents into one bucket, so $max returns a single overall maximum for the entire filtered collection.
If some documents have values and others are null or missing, $max ignores the null/missing ones. If every document lacks a usable value, $max returns null.
$max finds the largest value anywhere in the group. $last returns a field from the last document in pipeline order—they differ when the peak value is not on the last document.
$max returns the highest value; $min returns the lowest. Use both in the same $group for range summaries (min score and max score per class).
Did you know?
$max and $min use BSON comparison order, so dates, strings, and numbers compare by type rules—not just numeric magnitude. For the N largest numbers specifically, MongoDB 5.2+ offers $maxN. See the official $max docs.