View a markdown version of this page

$denseRank - Amazon DocumentDB

$denseRank

New from version 8.0.2.

The $denseRank window operator in Amazon DocumentDB returns the position of each document within its partition, according to the $setWindowFields stage sortBy order. Documents with the same sortBy value receive the same rank, and the next distinct value uses the immediately following position, leaving no gaps (for example, 1, 2, 2, 3). 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

  • $denseRank takes an empty document {} as its value.

  • It does not accept a window frame.

  • It requires exactly one sortBy key in the $setWindowFields stage.

Example (MongoDB Shell)

The following example partitions the documents by game, sorts each partition by score in descending order, and assigns each player a dense rank.

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: { denseRank: { $denseRank: {} } } } } ]);

Output

[ { "_id": 1, "game": "A", "player": "Alice", "score": 90, "denseRank": 1 }, { "_id": 2, "game": "A", "player": "Bob", "score": 85, "denseRank": 2 }, { "_id": 3, "game": "A", "player": "Carol", "score": 85, "denseRank": 2 }, { "_id": 4, "game": "A", "player": "Dave", "score": 70, "denseRank": 3 }, { "_id": 5, "game": "B", "player": "Erin", "score": 95, "denseRank": 1 }, { "_id": 6, "game": "B", "player": "Frank", "score": 80, "denseRank": 2 } ]

Within each game partition, players are ranked by descending score. Bob and Carol tie at 85 and share rank 2, and Dave follows at rank 3 with no gap.

Code examples

To view a code example for using the $denseRank operator in the $setWindowFields stage, choose the tab for the language that you want to use:

Node.js
const { MongoClient } = require('mongodb'); async function example() { const client = new MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); try { await client.connect(); const db = client.db('test'); const players = db.collection('players'); await 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 } ]); const result = await players.aggregate([ { $setWindowFields: { partitionBy: "$game", sortBy: { score: -1 }, output: { denseRank: { $denseRank: {} } } } } ]).toArray(); console.log(result); } finally { await client.close(); } } example();
Python
from pymongo import MongoClient def example(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') try: db = client['test'] players = db['players'] players.insert_many([ { '_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 } ]) result = list(players.aggregate([ { '$setWindowFields': { 'partitionBy': '$game', 'sortBy': { 'score': -1 }, 'output': { 'denseRank': { '$denseRank': {} } } } } ])) print(result) finally: client.close() example()