- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $bottomN (aggregation accumulator)
$bottomN (aggregation accumulator)¶
On this page
Definition¶
-
$bottomN
¶ New in version 5.2.
Returns an aggregation of the bottom
n
elements within a group, according to the specified sort order. If the group contains fewer thann
elements,$bottomN
returns all elements in the group.
Syntax¶
n
limits the number of results per group and has to be a positive integral expression that is either a constant or depends on the_id
value for$group
.- sortBy specifies the order of results, with syntax similar to
$sort
. output
represents the output for each element in the group and can be any expression.
Behavior¶
Null and Missing Values¶
$bottomN
does not filter out null values.$bottomN
converts missing values to null which are preserved in the output.
In this example:
$documents
creates the literal documents that contain player scores.$group
groups the documents bygameId
. This example has only onegameId
,G1
.PlayerD
has a missing score andPlayerE
has a nullscore
. These values are both considered as null.- The
playerId
andscore
fields are specified asoutput : ["$playerId"," $score"]
and returned as array values. - Because of the
sortBy: { "score" : -1 }
, the null values are sorted to the end of the returnedplayerId
array.
BSON Data Type Sort Ordering¶
When sorting different types, the order of BSON data types is used to determine ordering. As an example, consider a collection whose values consist of strings and numbers.
- In an ascending sort, string values are sorted after numeric values.
- In a descending sort, string values are sorted before numeric values.
In this example:
PlayerA
has an integer score.PlayerB
has a string"2"
score.PlayerC
has an empty string score.
Because the sort is in descending { "score" : -1 }
, the string
literal values are sorted before PlayerA
’s numeric score:
Restrictions¶
Window Function and Aggregation Expression Support¶
$bottomN
is not supported as a
aggregation expression.
$bottomN
is supported as a
window operator
.
Memory Limit Considerations¶
Groups within the $bottomN
aggregation pipeline are subject to the
100 MB limit pipeline limit. If this
limit is exceeded for an individual group, the aggregation fails
with an error.
Examples¶
Consider a gamescores
collection with the following documents:
Find the Three Lowest Scores
¶
You can use the $bottomN
accumulator to find the lowest scoring
players in a single game.
The example pipeline:
- Uses
$match
to filter the results on a singlegameId
. In this case,G1
. - Uses
$group
to group the results bygameId
. In this case,G1
. - Uses sort by
{ "score": -1 }
to sort the results in descending order. - Specifies the fields that are output from
$bottomN
withoutput : ["$playerId"," $score"]
. - Uses
$bottomN
to return the bottom three documents with the lowestscore
for theG1
game withn : 3
.
The operation returns the following results:
The SQL equivalent to this query is:
Finding the Three Lowest Score Documents Across Multiple Games¶
You can use the $bottomN
accumulator to find the lowest scoring
players in each game.
The example pipeline:
- Uses
$group
to group the results bygameId
. - Specifies the fields that are output from
$bottomN
withoutput : ["$playerId", "$score"]
. - Uses sort by
{ "score": -1 }
to sort the results in descending order. - Uses
$bottomN
to return the bottom three documents with the lowestscore
for each game withn: 3
.
The operation returns the following results:
The SQL equivalent to this query is:
Computing n
Based on the Group Key for $group
¶
You can also assign the value of n
dynamically. In this example,
the $cond
expression is used on the gameId
field.
The example pipeline:
- Use
$group
to group the results bygameId
. - Specifies the fields that are output from
$bottomN
withoutput : "$score"
. - If the
gameId
isG2
thenn
is 1, otherwisen
is 3. - Uses sort by
{ "score": -1 }
to sort the results in descending order.
The operation returns the following results: