- 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:
$rankand$denseRankreturn the same position (known as the rank) for those documents.$documentNumberreturns 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 forCAandWA.sortBy: { quantity: -1 }sorts the documents in each partition byquantityin descending order (-1), so the highestquantityis first.outputsets the document number in a new field calleddocumentNumberForStateshown in the following results.documentNumberForStateis unique within eachstatepartition.
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
quantityandstateas document 5. - Document 9 has the same
quantityandstateas document 4. - Document 10 has a
nullquantity. - 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 forCAandWA.sortBy: { quantity: -1 }sorts the documents in each partition byquantityin descending order (-1), so the highestquantityis first.outputsets the document number in a new field calleddocumentNumberForStateshown in the following results.documentNumberForStateis unique within eachstatepartition, and there aredocumentNumberForStatevalues for documents withnullquantityand missingquantityvalues.