The $minN accumulator collects the N smallest values in each $group bucket—ideal for bottom-3 scores, lowest prices, or floor metrics without a separate $sort stage.
01
N min per group
Array of lowest values.
02
input + n
Two required properties.
03
No $sort
Compares across the group.
04
vs $min
Many floors vs one floor.
05
Nulls filtered
Skips null and missing.
06
vs $bottomN
Min values vs ranked docs.
Fundamentals
Definition and Usage
$minN is a MongoDB accumulator used in the $group stage. For each group, it evaluates an input expression on every document, keeps the N smallest results, and returns them as an array (typically lowest first).
💡
Beginner tip
Think of $min as “give me the single lowest score” and $minN as “give me the bottom three scores.” Unlike $firstN, $minN ignores null and missing values automatically.
Use $minN for bottom-N reports, cheapest-price lists, and quality floor analysis. When you need guaranteed tie-breaking order or full documents at each rank, prefer $bottomN with an explicit sortBy.
Foundation
📝 Syntax
As a $group accumulator, $minN requires input and n:
Use tuple input: ["$score", "$playerId"] to attach context to each low value
Combine $min + $minN when dashboards need floor and bottom-N together
Put $match before $group to shrink groups and avoid memory pressure
Use $bottomN when tie order must follow explicit sortBy rules
Test with groups that have fewer than n valid values to confirm array length
❌ Don’t
Confuse $minN with $min (array vs single value)
Expect null scores to appear in the result—they are filtered out
Assume output order on tied equal values—use $bottomN for strict ordering
Compare mixed BSON types without understanding type ordering rules
Deploy on MongoDB versions before 5.2 without a fallback pipeline
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $minN
Use these points when building bottom-N min reports inside $group.
5
Core concepts
📉01
Bottom N values
Smallest per group.
Basics
📄02
input + n
Two required keys.
Syntax
📊03
No $sort
Full group compare.
Pattern
👤04
Nulls out
Filtered automatically.
Behavior
📥05
vs $bottomN
Min vs ranked docs.
Choose
❓ Frequently Asked Questions
$minN returns an array of the N smallest input values from each $group bucket. It compares values across all documents in the group and keeps the bottom N—no $sort stage required.
Inside $group: { bottomThree: { $minN: { input: "$score", n: 3 } } }. Both input and n are required. It also works as an aggregation expression and window operator.
$min returns a single lowest value. $minN returns an array of up to N smallest values. Use $min for the floor alone; use $minN for a bottom-N list of scores or prices.
$minN finds the N minimum values by comparison without requiring sortBy. $bottomN ranks inside $group with sortBy and returns the bottom N documents. Use $bottomN when you need a guaranteed sort order on ties.
No. Unlike $firstN and $lastN, $minN filters out null and missing values. If only three documents have scores and n is 4, you get an array of three elements.
$minN returns all available non-null values—never more than exist. A group with two numeric scores and n: 3 yields an array of two items.
Did you know?
$minN and $maxN were added in MongoDB 5.2 alongside $firstN, $lastN, $topN, and $bottomN—replacing fragile sort-and-slice recipes for bottom-N numeric reports. See the official $minN docs.