The $first accumulator picks the value from the first document in each $group bucket—ideal for earliest sale dates, first order IDs, or opening prices when you control order with $sort.
01
First per group
Value from the leading doc.
02
Simple syntax
{ $first: "$field" }
03
Needs $sort
Order before $group matters.
04
vs $last
First vs last in order.
05
vs $bottom
Stream order vs in-group rank.
06
Null handling
Missing fields on first doc.
Fundamentals
Definition and Usage
$first is a MongoDB accumulator used in $group (and related stages). It evaluates an expression on the first document that enters each group and returns that result.
💡
Beginner tip
$first does not sort documents itself. Without a preceding $sort, “first” means whatever order MongoDB happens to process—often unpredictable. Always sort first when you care about earliest, cheapest, or top-ranked records.
Common pattern: $sort by date ascending, then $group with { firstSale: { $first: "$date" } } to get the earliest sale per product. For ranked picks inside the group without a global sort, consider $bottom or $topN instead.
Foundation
📝 Syntax
$first takes a single expression—the field or computed value to capture from the first document:
$first is the straightforward way to grab a value from the leading document in each $group bucket. Pair it with $sort so “first” matches your business meaning—earliest sale, first login, opening price.
Combine with $last for date ranges, or move to $firstN when you need several leading documents per group.
Add $sort immediately before $group when order matters
Include tie-break sort keys (_id, name) for stable ordering
Use $first + $last together for per-group time ranges
Use $$ROOT when you need the full first record, not one field
Put $match before $sort to reduce documents sorted
❌ Don’t
Assume $first returns the minimum value—it follows document order
Skip $sort and expect consistent “earliest” results
Confuse $first with $firstN (one value vs array of N)
Expect $first to skip null on later documents—it only reads the first doc
Use $first for ranked picks when $bottom/$top fit better
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $first
Use these points when capturing leading values inside $group.
5
Core concepts
📄01
First doc
Leading value per group.
Basics
📅02
$sort first
Define what first means.
Pattern
🔄03
vs $last
Range endpoints.
Compare
📦04
$$ROOT
Full first document.
Output
⚠️05
Null on miss
First doc missing field.
Edge case
❓ Frequently Asked Questions
$first returns the value of an expression from the first document in each $group bucket—based on the order documents arrive at $group. Use it for earliest dates, first order IDs, or opening prices per category.
Yes, in almost every real pipeline. $first does not sort for you—it takes whatever document is first in the current order. Add a $sort stage before $group so first means what you expect (e.g. earliest date).
$first reads the first document in each group; $last reads the last. With $sort: { date: 1 }, $first gives the earliest date and $last gives the latest.
$first follows pipeline document order (usually set by $sort). $bottom ranks inside $group with its own sortBy and picks one bottom-ranked document. Use $bottom for explicit ranking; use $first when you already sorted the stream.
$first returns null when the expression resolves to a missing field on that first document. Missing values are not skipped—the first doc is still used.
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?
Before MongoDB 5.2’s $top/$bottom family, the classic recipe for “best record per group” was $sort + $group + $first. That pattern still works—and $first remains essential for time-series open/close values. See the official $first docs.