The $bottomN accumulator collects the N lowest-ranked documents in each $group bucket—perfect for bottom-3 leaderboards, cheapest products, or earliest orders when one record is not enough.
01
N docs per group
Array of bottom-ranked records.
02
n + sortBy + output
Three required properties.
03
Flexible output
Project fields or use arrays.
04
vs $bottom
Many records vs one record.
05
Short groups OK
Returns all docs if fewer than N.
06
MongoDB 5.2+
Part of $top/$bottom family.
Fundamentals
Definition and Usage
$bottomN is a MongoDB accumulator used in the $group stage. It sorts documents within each group using sortBy, then returns the last N documents in that ordering as an array.
💡
Beginner tip
“Bottom” means the end of the sorted list—not automatically “smallest number.” To get the three lowest scores, MongoDB docs use sortBy: { score: -1 } (descending). High scores sort first; the bottom three slots hold the lowest values.
Choose $bottomN when dashboards need a tail of ranked records (bottom 5 sellers, 3 slowest response times). Use $bottom when only the single worst record matters, or $topN for the top of the ranking.
Foundation
📝 Syntax
$bottomN requires an object with n, sortBy, and output:
$bottomN answers “which N records sit at the low end of my ranking?” inside each group. Set n, define ranking with sortBy, and shape results with output.
Pair with $bottom for the single worst record, or explore $first when input order matters instead of explicit ranking.
Document your sortBy choice—“bottom” follows sort order, not intuition alone
Use sortBy: { score: -1 } for lowest numeric scores (MongoDB official pattern)
Add secondary sort keys (playerId, _id) to break ties
Put $match before $group to reduce data processed per bucket
Test with groups smaller than n to confirm array length behavior
❌ Don’t
Confuse $bottomN with $bottom (array vs single object)
Assume ascending sort always means “lowest score”—verify against your sortBy
Expect $min or $minN to return player names—they return values only
Use on MongoDB versions before 5.2 without a fallback pipeline
Forget the 100 MB per-group memory cap on very large buckets
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $bottomN
Use these points when building bottom-k reports inside $group.
5
Core concepts
📦01
Array of N
Bottom-ranked docs per group.
Basics
🔢02
n key
How many to collect.
Syntax
⬇️03
sortBy
Defines bottom ranking.
Ranking
👤04
vs $bottom
Many vs one record.
Compare
📄05
5.2+
Check server version.
Version
❓ Frequently Asked Questions
$bottomN returns an array of the N documents ranked at the bottom of each $group bucket according to sortBy. Use it for bottom-3 scorers, lowest-priced products, or earliest N orders—when you need more than one low-ranked record.
$bottom returns exactly one bottom-ranked document (a single object). $bottomN returns up to N documents as an array. Use $bottom for the single worst record; use $bottomN for a leaderboard tail or bottom-k list.
MongoDB ranks documents, then takes the last N in that order. For lowest numeric scores, use sortBy: { score: -1 } (descending)—high scores first, lowest scores at the bottom. Always match sortBy to your business definition of bottom.
$bottomN returns all documents in the group—never more than exist. If a class has only two students and n is 3, you get an array of two items.
$bottomN was added in MongoDB 5.2 alongside $top, $bottom, and $topN. On older servers, approximate with $sort + $group patterns (less efficient).
Did you know?
$bottomN can use a dynamic n expression based on the group _id—handy when one pipeline must return different tail sizes per category. See the official $bottomN docs.