- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $documentNumber (aggregation)
$documentNumber (aggregation)¶
On this page
Definition¶
New in version 5.0.
-
$documentNumber
¶
Returns the position of a document (known as the document number) in the
$setWindowFields
stage partition.
The $setWindowFields
stage sortBy field determines the document number. For
more information on how MongoDB compares fields with different types,
see BSON comparison order.
$documentNumber
returns a unique number for each document in a
partition, even if multiple
documents have identical sortBy field
values in the partition.
$documentNumber
is only available in the
$setWindowFields
stage.
$documentNumber
syntax:
$documentNumber
does not accept any parameters.
Behavior¶
$documentNumber
includes documents that have a sortBy field that is null
or missing.
$documentNumber
, $rank
, and $denseRank
return
the position of the documents based on the sortBy field values.
$documentNumber
differs from $rank
and
$denseRank
in how documents with identical sortBy field values in a partition are treated:
$rank
and$denseRank
return the same position (known as the rank) for those documents.$documentNumber
returns a unique position (known as the document number) for those documents.
See the example in Document Number for Duplicate, Null, and Missing Values.
Examples¶
Document Number for Each State¶
Create a cakeSales
collection that contains cake sales in the states
of California (CA
) and Washington (WA
):
This example uses $documentNumber
in the
$setWindowFields
stage to output the cake sales document
number for each state
:
In the example:
partitionBy: "$state"
partitions the documents in the collection bystate
. There are partitions forCA
andWA
.sortBy: { quantity: -1 }
sorts the documents in each partition byquantity
in descending order (-1
), so the highestquantity
is first.output
sets the document number in a new field calleddocumentNumberForState
shown in the following results.documentNumberForState
is unique within eachstate
partition.
Document Number for Duplicate, Null, and Missing Values¶
Create a cakeSalesWithDuplicates
collection where:
- Cake sales are placed in the state of California (
CA
) and Washington (WA
). - Documents 6 to 8 have the same
quantity
andstate
as document 5. - Document 9 has the same
quantity
andstate
as document 4. - Document 10 has a
null
quantity
. - Document 11 is missing the
quantity
.
This example uses $documentNumber
in the
$setWindowFields
stage to output the
cakeSalesWithDuplicates
document number for each state
:
In the example:
partitionBy: "$state"
partitions the documents in the collection bystate
. There are partitions forCA
andWA
.sortBy: { quantity: -1 }
sorts the documents in each partition byquantity
in descending order (-1
), so the highestquantity
is first.output
sets the document number in a new field calleddocumentNumberForState
shown in the following results.documentNumberForState
is unique within eachstate
partition, and there aredocumentNumberForState
values for documents withnull
quantity
and missingquantity
values.