$bottom
New from version 8.0.1.
Use the $bottom accumulator in the $group stage to return the lowest-ranked document per group according to a specified sort order.
Parameters
-
sortBy: A document specifying the sort order. Use1for ascending or-1for descending. -
output: An expression that specifies the fields to return from the bottom document.
Example (MongoDB Shell)
The following example shows how to use the $bottom accumulator to find the bottom sale (lowest quantity) per item in a sales collection.
Create sample documents
db.sales.insertMany([ { item: "abc", quantity: 10, price: 5 }, { item: "abc", quantity: 5, price: 8 }, { item: "xyz", quantity: 15, price: 3 }, { item: "xyz", quantity: 7, price: 6 } ])
Query example
db.sales.aggregate([ { $group: { _id: "$item", bottomSale: { $bottom: { sortBy: { quantity: -1 }, output: { quantity: "$quantity", price: "$price" } } } } } ])
Output
[
{ "_id": "xyz", "bottomSale": { "quantity": 7, "price": 6 } },
{ "_id": "abc", "bottomSale": { "quantity": 5, "price": 8 } }
]
Code examples
To view a code example for using the $bottom operator, choose the tab for the language that you want to use: