$top/$topN/$bottom/$bottomN
Reference group: Accumulators and ranking
What it does
Selects top/bottom values or documents per group in supported versions.
Syntax
Accumulator with sortBy/output
Use case
Top-selling items.
Important notes
Check server-version support.
$top/$topN/$bottom/$bottomN: practical example
{ $group: { _id: "$category", topProduct: { $top: { sortBy: { sales: -1 }, output: "$$ROOT" } } } }💡 Explanation
Selects top/bottom values or documents per group in supported versions.
When to use it
Top-selling items.
Caution
Check server-version support.
Key Takeaways
- Selects top/bottom values or documents per group in supported versions.
- Typical use: Top-selling items.