- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $first (aggregation accumulator)
$first (aggregation accumulator)¶
On this page
Definition¶
-
$first¶
Changed in version 5.0.
Returns the value that results from applying an expression to the first document in a group of documents. Only meaningful when documents are in a defined order.
$first is available in these stages:
$bucket$bucketAuto$group$setWindowFields(Available starting in MongoDB 5.0)
Note
Disambiguation
This page describes the $first aggregation accumulator. For
the $first array operator, see $first
(array operator).
Behavior¶
To define the document order for $first with the:
$groupstage, add a$sortstage before the$groupstage.$setWindowFieldsstage, set the sortBy field.
Note
Although the $sort stage passes ordered documents as
input to the $group and $setWindowFields
stages, those stages are not guaranteed to maintain the sort order in
their own output.
When used with $setWindowFields, $first returns null
for empty windows. An example empty
window is a { documents: [ -1, -1 ] } documents window on the first document of a
partition.
Missing Values¶
The documents in a group may be missing fields or may have fields with missing values.
- If there are no documents from the prior pipeline stage, the
$groupstage returns nothing. - If the field that the
$firstaccumulator is processing is missing,$firstreturnsnull.
See the missing data example.
Examples¶
Use in $group Stage¶
Create the sales collection:
Grouping the documents by the item field, the following operation
uses the $first accumulator to compute the first sales date for
each item:
The operation returns the following results:
Missing Data¶
Some documents in the badData collection are missing fields, other
documents are missing values.
Create the badData collection:
Query the badData collection, grouping the output on the item
field:
The $sort stage orders the documents and passes them to the
$group stage.
$first selects the first document from each output group:
- The
_id: nullgroup is included. - When the accumulator field,
$quantityin this example, is missing,$firstreturnsnull.
Use in $setWindowFields Stage¶
New in version 5.0.
Create a cakeSales collection that contains cake sales in the states
of California (CA) and Washington (WA):
This example uses $first in the $setWindowFields
stage to output the first cake sales order type for each state:
In the example:
partitionBy: "$state"partitions the documents in the collection bystate. There are partitions forCAandWA.sortBy: { orderDate: 1 }sorts the documents in each partition byorderDatein ascending order (1), so the earliestorderDateis first.outputsets thefirstOrderTypeForStatefield to the first ordertypefrom the documents window.The window contains documents between an
unboundedlower limit and thecurrentdocument in the output. This means$firstreturns the first ordertypefor the documents between the beginning of the partition and the current document.
In this output, the first order type value for CA and WA is
shown in the firstOrderTypeForState field: