The $bottom accumulator picks the single lowest-ranked document in each $group bucket using a sortBy specification—ideal for “worst score,” “earliest order,” or “lowest price” reports.
01
One doc per group
Full record at bottom of sort.
02
sortBy required
Defines what “bottom” means.
03
output shape
$$ROOT or selected fields.
04
vs $min
Value only vs whole document.
05
vs $bottomN
One record vs N records.
06
MongoDB 5.2+
Part of $top/$bottom family.
Fundamentals
Definition and Usage
$bottom is a MongoDB accumulator used in the $group stage. It sorts documents within each group according to sortBy, then returns the document at the bottom of that ordering (the last one if you sorted ascending, or the minimum rank depending on your sort keys).
💡
Beginner tip
With sortBy: { score: 1 }, ascending order puts the smallest score at the bottom—so $bottom returns the student with the lowest score in each class. Use score: -1 when “bottom” should mean the lowest rank in a descending list.
Choose $bottom when you need identifying fields (name, id, date) from the extreme record. Use $min when you only need the numeric minimum. Need multiple low-ranked rows? Use $bottomN.
Foundation
📝 Syntax
$bottom requires an object with output and sortBy:
$bottom answers “which single record is at the low end of my ranking?” inside each group. Define sortBy carefully, project with output, and add tie-breakers when duplicates matter.
Pair with $min for the numeric extreme alone, or move to $bottomN when you need several bottom-ranked documents per bucket.
Always specify sortBy explicitly—match your business definition of “bottom”
Add secondary sort keys (student, _id) to break ties
Use $$ROOT when downstream stages need the full document
Put $match before $group to shrink working sets
Confirm MongoDB 5.2+ in staging before production rollout
❌ Don’t
Confuse $bottom with $bottomN (one doc vs array)
Expect $min to return student names—it only returns the minimum value
Rely on tie order without extra sortBy fields
Use on MongoDB versions before 5.2 without a fallback pipeline
Forget that sort direction changes which document is “bottom”
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $bottom
Use these points when ranking documents inside $group.
5
Core concepts
⬇️01
One document
Bottom of sort per group.
Basics
📦02
sortBy
Required ranking spec.
Syntax
👤03
vs $min
Doc vs scalar value.
Compare
📄04
$$ROOT
Return full record.
Pattern
🔢05
5.2+
Check server version.
Version
❓ Frequently Asked Questions
$bottom returns one document per $group—the document ranked last according to the sortBy order you specify. It is useful when you need the whole record (name, score, date), not just a single field value.
Inside the $group stage: { lowest: { $bottom: { output: "$$ROOT", sortBy: { score: 1 } } } }. It requires both output and sortBy.
$min returns the smallest value of one field (e.g. the number 72). $bottom returns an entire document—the student or order that sits at the bottom of your sort order.
$bottom returns exactly one bottom-ranked document. $bottomN returns N bottom documents as an array. Use $bottom when you only need the single worst/lowest record.
$bottom was added in MongoDB 5.2 as part of the $top/$bottom accumulator family. Upgrade the server or use $sort + $first/$last patterns on older versions.
sortBy defines ranking within each group—same rules as $sort. Use { score: 1 } for lowest score first (bottom = smallest score), or { date: 1 } for earliest date at the bottom.
Did you know?
$bottom and $top were added together in MongoDB 5.2 to replace fragile $sort + $group + $first recipes for ranked group results. See the official $bottom docs.