The $last accumulator picks the value from the last document in each $group bucket—ideal for latest sale dates, most recent orders, or closing prices when you control order with $sort.
01
Last per group
Value from the trailing doc.
02
Simple syntax
{ $last: "$field" }
03
Needs $sort
Order before $group matters.
04
vs $first
Last vs first in order.
05
vs $max
Last doc vs max value.
06
Null handling
Missing fields on last doc.
Fundamentals
Definition and Usage
$last is a MongoDB accumulator used in $group (and related stages). It evaluates an expression on the last document that enters each group and returns that result.
💡
Beginner tip
$last does not sort documents itself. Without a preceding $sort, “last” means whatever order MongoDB happens to process—often unpredictable. Always sort first when you need the latest date, highest price in sequence, or most recent record.
Common pattern: $sort by date ascending, then $group with { lastSale: { $last: "$date" } } to get the most recent sale per product. Pair with $first for open/close date ranges. For ranked picks inside the group, consider $topN.
Foundation
📝 Syntax
$last takes a single expression—the field or computed value to capture from the last document:
$last is the straightforward way to grab a value from the trailing document in each $group bucket. Pair it with $sort so “last” matches your business meaning—latest sale, most recent status, closing price.
Combine with $first for date ranges, or move to $lastN when you need several trailing documents per group.
Add $sort immediately before $group when order matters
Include tie-break sort keys (_id, name) for stable last-document picks
Use $first + $last together for per-group time ranges
Use $$ROOT when downstream stages need the full last record
Put $match before $sort to reduce documents sorted
❌ Don’t
Assume $last returns the maximum value—it follows document order
Skip $sort and expect consistent “latest” results
Confuse $last with $lastN (one value vs array of N)
Expect $last to skip null on earlier documents—it only reads the last doc
Use $last for ranked picks when $top/$bottom fit better
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $last
Use these points when capturing trailing values inside $group.
5
Core concepts
📄01
Last doc
Trailing value per group.
Basics
📅02
$sort first
Define what last means.
Pattern
🔄03
vs $first
Range endpoints.
Compare
📈04
vs $max
Last doc vs peak value.
Compare
⚠️05
Null on miss
Last doc missing field.
Edge case
❓ Frequently Asked Questions
$last returns the value of an expression from the last document in each $group bucket—based on the order documents arrive at $group. Use it for latest dates, most recent order IDs, or closing prices per category.
Yes, in almost every real pipeline. $last does not sort for you—it takes whatever document is last in the current order. Add a $sort stage before $group so last means what you expect (e.g. latest date).
$last reads the final document in each group; $first reads the leading one. With $sort: { date: 1 }, $first gives the earliest date and $last gives the latest.
$max returns the largest numeric value across all documents in a group. $last returns a field from the last document in pipeline order—they can differ if the highest value is not on the last document.
$last returns null when the expression resolves to a missing field on that last document. It does not fall back to earlier documents in the group.
As an accumulator in $group, $bucket, and $bucketAuto. It is also a window operator in $setWindowFields (MongoDB 5.0+) with sortBy on the window.
Did you know?
Sorting by date descending and using $first gives the same result as sorting ascending and using $last—pick whichever reads clearer in your pipeline. See the official $last docs.