$max is a MongoDB accumulator for the $group stage. For each group it returns the highest value of an expression across all documents in that bucket. No $sort is required.
Remember
$max = highest value per group
scan every document in the group
compare values → keep the larger one
null / missing → ignored when other values exist
vs $min → lowest value
vs $last → value from last doc (order-based, not peak)
Pass a field path (e.g. "$amount") or any comparable expression.
Set _id to the group key, or null for one overall maximum.
Optionally $match first so you maximize only the rows you care about.
Piece
Required?
Role
$group._id
Yes
Group key ("$category", null, …)
$max
Yes
Expression to maximize
Expression
Yes
Usually "$field"; may be computed
Pseudocode
Pseudocode
for each group:
best = null
for each document in group:
value = evaluate(expression)
if value is comparable:
if best is null or value > best:
best = value
emit { _id: groupKey, field: best }
Reference
Cheat Sheet
Keep these facts handy while writing a maximum rollup.
Topic
Detail
Stage
$group accumulator (also usable on arrays in $project)
Syntax
{ $max: "$field" }
Needs $sort?
No — compares all values in the group
null / missing
Ignored when other values exist
Empty group
Returns null when no comparable values
Sample check
food 100, 150, 50 → max 150
Try it
Live Preview
Watch the running maximum update. Sample collection: food 100, food 150, travel 200, food 50.
Simulates keeping the larger amount as each document is seen (same idea as the mongosh examples below).
Result
+ 100 (food) → max 100
+ 150 (food) → max 150
+ 200 (travel) → max 200
+ 50 (food) → max 200
{ _id: null, maxAmount: 200 }
Trace
Worked Walkthrough — Overall Maximum
Same four purchases as the examples. The peak amount across all docs is 200.
We use a tiny shopping list of four purchases. First we find the overall maximum. Then we find the maximum per category. Run these in mongosh after inserting the sample data once.
Example 1 — Sample Data
Imagine four receipts in a transactions collection. Each document has an amount and a category.
Sort for stable reading.$sort orders the group keys alphabetically.
Pair with $min for a range. Add { $min: "$amount" } in the same $group to get low and high together.
Edge Cases & Pitfalls
Check these before shipping a maximum report.
null
Null and missing are skipped
They do not win the comparison when other values exist. All-null groups return null.
$last
Do not confuse with $last
$max answers “what is the largest value?” $last answers “what is on the last document?”
types
Comparison uses BSON order
Mixed types compare by BSON type ranking. Prefer a single numeric (or date) type for clear results.
doc
$max returns a value, not a document
To return the whole document that holds the peak, use $top with sortBy instead.
Analysis
Performance Notes
Approach
Cost
Best for
$max
Native, one pass per group
Single highest value
$maxN
Keeps N largest values
Top-N amounts as an array
$sort + $last
Sort cost
When you need a field from the last-sorted doc, not the peak
Prefer native $max over sorting just to find a peak. Filter early with $match so fewer documents enter the group.
Remember
Key Takeaways
Role: highest value inside each $group bucket.
Order: no $sort needed—compares all values.
Nulls: ignored when other values exist; all-null → null.
vs $last: peak value vs field from the last document.
One line:$max is the built-in peak finder for each $group bucket—scan, compare, keep the largest.
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 as an accumulator, for example { maxAmount: { $max: "$amount" } }. You can also use $max on arrays in $project, but this page focuses on the $group form.
Grouping with _id: null puts every document in one bucket, so $max returns a single overall maximum for the whole input.
Null and missing values are ignored when other comparable values exist. If every value in a group is null or missing, $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 is not on that last document.
$max returns the highest value; $min returns the lowest. Use both in the same $group for range summaries.