$documentNumber
New from version 8.0.2.
The $documentNumber window operator in Amazon DocumentDB returns a distinct sequential number for each document in its partition, according to the $setWindowFields stage sortBy order. Unlike $rank and $denseRank, it does not treat ties specially: every document receives a unique number (for example, 1, 2, 3, 4), and documents with equal sortBy values are numbered in the order they are encountered. It is specified under the output field of the $setWindowFields stage.
It is a window operator that is used only in the $setWindowFields stage.
Parameters
-
$documentNumbertakes an empty document{}as its value. -
It does not accept a
windowframe. -
It requires exactly one
sortBykey in the$setWindowFieldsstage.
Example (MongoDB Shell)
The following example partitions the documents by game, sorts each partition by score in descending order, and assigns each player a sequential document number.
Create sample documents
db.players.insertMany([ { _id: 1, game: "A", player: "Alice", score: 90 }, { _id: 2, game: "A", player: "Bob", score: 85 }, { _id: 3, game: "A", player: "Carol", score: 85 }, { _id: 4, game: "A", player: "Dave", score: 70 }, { _id: 5, game: "B", player: "Erin", score: 95 }, { _id: 6, game: "B", player: "Frank", score: 80 } ]);
Query example
db.players.aggregate([ { $setWindowFields: { partitionBy: "$game", sortBy: { score: -1 }, output: { documentNumber: { $documentNumber: {} } } } } ]);
Output
[
{ "_id": 1, "game": "A", "player": "Alice", "score": 90, "documentNumber": 1 },
{ "_id": 2, "game": "A", "player": "Bob", "score": 85, "documentNumber": 2 },
{ "_id": 3, "game": "A", "player": "Carol", "score": 85, "documentNumber": 3 },
{ "_id": 4, "game": "A", "player": "Dave", "score": 70, "documentNumber": 4 },
{ "_id": 5, "game": "B", "player": "Erin", "score": 95, "documentNumber": 1 },
{ "_id": 6, "game": "B", "player": "Frank", "score": 80, "documentNumber": 2 }
]
Within each game partition, each player receives a distinct sequential number in descending score order. Bob and Carol tie at 85 but are still numbered 2 and 3 in the order they are encountered.
Code examples
To view a code example for using the $documentNumber operator in the $setWindowFields stage, choose the tab for the language that you want to use: