$bottom (Bottom Accumulator)
Last updated: 2026-10-09
$bottom (Bottom Accumulator)
$bottom is an accumulator returning the bottom document of a group per a specified sort order — the whole document — available since MongoDB 5.2, symmetric to $top. Order comes from $group's sortBy, not input sequence. Typical: lowest score or oldest record per group.
| Category | Accumulators |
|---|---|
| MongoDB Version | 5.2+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $group: { _id: <key>, bottom: { $bottom: { sortBy: { <field>: <1 or -1> }, output: <expression> } } } }
Returns:Bottom document of the group per the sortBy order
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $bottom: lowest price per item by sortBy
db.sales.aggregate([
{ $group: { _id: '$item', bottom: { $bottom: { sortBy: { price: 1 }, output: '$price' } } } }
])
Output:
{ "_id": "Pen", "bottom": 20 }
📌 Notes
- Requires MongoDB 5.2 or later
- Symmetric to $top; semantics depend on the sortBy direction
- Omit output to return the whole document
🏷️ Related Tags
💡 Related Cases
More hands-on cases coming soon.
📚 Related Tutorials
❓ FAQ
QWhat is the difference between $bottom and $min?
A$min returns the minimum value; $bottom returns the whole last document or expression result.
QHow do I get the oldest record?
AWith sortBy ascending by time, $bottom takes the oldest and $top the newest.
QWhich version does $bottom require?
AMongoDB 5.2 or later.