$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 } }
]
Window operator usage example (MongoDB Shell)
New from version 8.0.2.
The $bottom operator can also be used as a window operator in the $setWindowFields stage. In this context, it returns the output from the bottom-ranked document (according to the operator's own sortBy) among the documents in each window. You specify the operator under the output field, and optionally define the window boundaries with a window document.
Note
When used as a window operator in $setWindowFields, $bottom is limited to 100 MB of intermediate data. An operation that exceeds this limit returns an error.
Create sample documents
db.matchScores.insertMany([ { _id: 1, player: "Alice", round: 1, score: 20 }, { _id: 2, player: "Alice", round: 2, score: 35 }, { _id: 3, player: "Alice", round: 3, score: 28 }, { _id: 4, player: "Bob", round: 1, score: 15 }, { _id: 5, player: "Bob", round: 2, score: 40 } ]);
Query example
The following example partitions the documents by player, sorts each partition by round, and returns the lowest-scoring round seen from the start of the partition through the current document.
db.matchScores.aggregate([ { $setWindowFields: { partitionBy: "$player", sortBy: { round: 1 }, output: { worstRoundSoFar: { $bottom: { sortBy: { score: -1 }, output: { round: "$round", score: "$score" } }, window: { documents: ["unbounded", "current"] } } } } } ]);
Output
[
{ "_id": 1, "player": "Alice", "round": 1, "score": 20, "worstRoundSoFar": { "round": 1, "score": 20 } },
{ "_id": 2, "player": "Alice", "round": 2, "score": 35, "worstRoundSoFar": { "round": 1, "score": 20 } },
{ "_id": 3, "player": "Alice", "round": 3, "score": 28, "worstRoundSoFar": { "round": 1, "score": 20 } },
{ "_id": 4, "player": "Bob", "round": 1, "score": 15, "worstRoundSoFar": { "round": 1, "score": 15 } },
{ "_id": 5, "player": "Bob", "round": 2, "score": 40, "worstRoundSoFar": { "round": 1, "score": 15 } }
]
Each document is augmented with worstRoundSoFar, the lowest-scoring round within its partition up to and including the current document.
Code examples
To view a code example for using the $bottom operator, choose the tab for the language that you want to use. The following examples show both accumulator usage (in $group) and window operator usage (in $setWindowFields):