Definition
Returns the result of an expression for the last document in a group of documents. Only meaningful when documents are in a defined order.
The $last accumulator is available in these stages:
Note
Disambiguation
This page describes $last when used as an accumulator. Accumulators return a single aggregated value like sum, maximum, or minimum across a group of input documents.
You can also use $last in these other contexts:
$last (expression), which returns the last element of an array.$last (window function), used in the$setWindowFieldsstage to return a value for the last document in a window.
Syntax
{ $last: <expression> }
Behavior
Define Document Order
To define document order for $last in a grouping stage, add a preceding $sort stage.
Missing Values
Documents in a group may be missing fields. $last handles missing values as follows:
If there are no documents from the prior pipeline stage, the
$groupstage returns nothing.When the
expressionfor the last document in the group evaluates to a missing value,$lastreturnsnull.
For example, consider the following documents in the badData collection, where some documents are missing fields:
db.badData.insertMany( [ { "_id": 1, "price": 6, "quantity": 6 }, { "_id": 2, "item": "album", "price": 5, "quantity": 5 }, { "_id": 7, "item": "tape", "price": 6, "quantity": 6 }, { "_id": 8, "price": 5, "quantity": 5 }, { "_id": 9, "item": "album", "price": 3, "quantity": '' }, { "_id": 10, "item": "tape", "price": 3, "quantity": 4 }, { "_id": 12, "item": "cd", "price": 7 } ] )
Query the badData collection, grouping the output on the item field:
db.badData.aggregate( [ { $sort: { item: 1, price: 1 } }, { $group: { _id: "$item", inStock: { $last: "$quantity" } } } ] )
$last selects the last document from each output group. Documents that are missing the item field are grouped together with a null value for the _id field. When the accumulator field is missing (quantity in this example), $last returns null.
Examples
The examples on this page use data from the sample_mflix dataset. For details on how to load this dataset into your self-managed MongoDB deployment, see Load the Sample Dataset. If you made any modifications to the sample databases, you may need to drop and recreate the databases to run the examples on this page.
The following example sorts movies by year and title, then groups them by genre to find the most recent movie released in each genre:
db.movies.aggregate( [ { $match: { year: { $type: "int" } } }, { $sort: { year: 1, title: 1 } }, { $unwind: "$genres" }, { $group: { _id: "$genres", lastMovieTitle: { $last: "$title" }, lastMovieYear: { $last: "$year" } } }, { $sort: { _id: 1 } }, { $limit: 5 } ] )
In the preceding pipeline:
$matchfilters to movies with an integeryearfield. This eliminates bad data whereyearis missing or not a number.$sortorders movies byyearin ascending order, thentitlein alphabetical order.$unwindcreates a separate document for each genre in thegenresarray, maintaining the previously established sort order.$groupwith$lastreturns thetitleandyearof the last movie (most recent by year and alphabetically last by title) for each genre.A second
$sortsorts the results alphabetically by genre.$limitrestricts the output to 5 genres.