- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $lastN (aggregation accumulator)
$lastN (aggregation accumulator)¶
On this page
Definition¶
-
$lastN¶ New in version 5.2.
Returns an aggregation of the last
nelements within a group. The elements returned are meaningful only if in a specified sort order. If the group contains fewer thannelements,$lastNreturns all elements in the group.
Syntax¶
inputspecifies the field(s) from the document to take the lastnof. Input can be any expression.nhas to be a positive integral expression that is either a constant or depends on the_idvalue for$group. For details see group key example.
Behavior¶
Null and Missing Values¶
$lastNdoes not filter out null values.$lastNconverts missing values to null.
Consider the following aggregation that returns the last five documents from a group:
In this example:
$documentscreates the literal documents that contain player scores.$groupgroups the documents bygameId. This example has only onegameId,G1.PlayerDhas a missing score andPlayerEhas a nullscore. These values are both considered as null.- The
lastFiveScoresfield is specified usinginput : "$score"and returned as an array. - Since there is no sort criteria the last 5
scorefields are returned.
Comparison of $lastN and $bottomN¶
Both $lastN and $bottomN accumulators can accomplish similar
results.
In general:
- If the documents coming into
$groupare already ordered, you should use$lastN. - If you’re sorting and selecting the bottom
nelements then you can use$bottomNto accomplish both tasks with one accumulator. $lastNcan be used as an aggregation expression,$bottomNcannot.
Restrictions¶
Window Function and Aggregation Expression Support¶
$lastN is supported as an
aggregation expression.
For details on aggregation expression usage see Using $lastN as an Aggregation Expression.
$lastN is supported as a
window operator.
Memory Limit Considerations¶
Aggregation pipelines which call $lastN are subject to the
100 MB 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 Last Three Player Scores for a Single Game¶
You can use the $lastN accumulator to find the last three scores
in a single game.
The example pipeline:
- Uses
$matchto filter the results on a singlegameId. In this case,G1. - Uses
$groupto group the results bygameId. In this case,G1. - Specifies the fields that are output from
$lastNwithoutput : ["$playerId"," $score"]. - Uses
$lastNto return the last three documents for theG1game withn : 3.
The operation returns the following results:
Finding the Last Three Player Scores Across Multiple Games¶
You can use the $lastN accumulator to find the last n
input fields in each game.
The example pipeline:
- Uses
$groupto group the results bygameId. - Uses
$lastNto return the last three documents for each game withn: 3. - Specifies the fields that are input for
$lastNwithinput : ["$playerId", "$score"].
The operation returns the following results:
Using $sort With $lastN¶
Using a $sort stage earlier in the pipeline can influence the
results of the $lastN accumulator.
In this example:
{$sort : { score : -1 } }sorts the highest scores to the back of each group.lastNreturns the three lowest scores from the back of each group.
The operation returns the following results:
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:
- Uses
$groupto group the results bygameId. - Specifies the fields that input for
$lastNwithinput : "$score". - If the
gameIdisG2thennis 1, otherwisenis 3.
The operation returns the following results:
Using $lastN as an Aggregation Expression¶
You can also use $lastN as an aggregation expression.
In this example:
$documentscreates the literal document that contains an array of values.$projectis used to return the output of$lastN._idis omited from the output with_id : 0.$lastNuses the input array of[10, 20, 30, 40].- The last three elements of the array are returned for the input document.
The operation returns the following results: