View a markdown version of this page

$percentile - Amazon DocumentDB

$percentile

New from version 8.0.1.

The $percentile operator in Amazon DocumentDB calculates specified percentile values for numeric data. As an accumulator, it computes percentile values across documents within a group in the $group stage of an aggregation pipeline. As an expression, it calculates percentiles for an array of numbers.

Parameters

  • input: An expression that resolves to a numeric value or an array of numeric values.

  • p: An array of percentile values between 0 and 1, where each value represents a percentile to calculate. For example, [0.25, 0.5, 0.75] calculates the 25th, 50th, and 75th percentiles.

  • method: A string specifying the calculation method. Currently only "approximate" is supported.

Behavior

The "approximate" method uses the t-digest algorithm to calculate approximate, percentile-based metrics. With small datasets, distinct percentile values may resolve to the same result. For example, with only 5 values per group, p90 and p99 may both return the maximum value in the group. Precision improves as the number of data points increases.

Example (MongoDB Shell)

The following example shows how to use the $percentile operator to calculate the 25th and 75th percentile of scores per class.

Create sample documents

db.students.insertMany([ { class: "A", score: 72 }, { class: "A", score: 85 }, { class: "A", score: 90 }, { class: "A", score: 68 }, { class: "A", score: 95 }, { class: "B", score: 80 }, { class: "B", score: 75 }, { class: "B", score: 92 }, { class: "B", score: 88 }, { class: "B", score: 70 } ]);

Query example

db.students.aggregate([ { $group: { _id: "$class", percentiles: { $percentile: { input: "$score", p: [0.25, 0.75], method: "approximate" } } }} ]);

Output

[ { "_id": "A", "percentiles": [72, 90] }, { "_id": "B", "percentiles": [75, 88] } ]

Expression usage example (MongoDB Shell)

The $percentile operator can also be used as an expression within a $project stage to compute percentiles of an array field.

Create sample documents

db.surveys.insertMany([ { _id: 1, responses: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] }, { _id: 2, responses: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] } ]);

Query example

db.surveys.aggregate([ { $project: { quartiles: { $percentile: { input: "$responses", p: [0.25, 0.5, 0.75], method: "approximate" } } }} ]);

Output

[ { "_id": 1, "quartiles": [6, 10, 16] }, { "_id": 2, "quartiles": [5, 9, 15] } ]

Window operator usage example (MongoDB Shell)

New from version 8.0.2.

The $percentile operator can also be used as a window operator in the $setWindowFields stage. In this context, it computes the specified percentile values for the documents in each window. You specify the operator under the output field, and optionally define the window boundaries with a window document.

Note

When used as a window operator in $setWindowFields, $percentile is limited to 100 MB of intermediate data. An operation that exceeds this limit returns an error.

Create sample documents

db.sensorData.insertMany([ { _id: 1, sensor: "A", reading: 72 }, { _id: 2, sensor: "A", reading: 85 }, { _id: 3, sensor: "A", reading: 90 }, { _id: 4, sensor: "A", reading: 68 }, { _id: 5, sensor: "A", reading: 95 }, { _id: 6, sensor: "B", reading: 80 }, { _id: 7, sensor: "B", reading: 75 }, { _id: 8, sensor: "B", reading: 92 }, { _id: 9, sensor: "B", reading: 88 }, { _id: 10, sensor: "B", reading: 70 } ]);

Query example

The following example partitions the documents by sensor and computes the 25th and 75th percentiles of reading across all documents in each partition.

db.sensorData.aggregate([ { $setWindowFields: { partitionBy: "$sensor", output: { readingPercentiles: { $percentile: { input: "$reading", p: [0.25, 0.75], method: "approximate" }, window: { documents: ["unbounded", "unbounded"] } } } } } ]);

Output

[ { "_id": 1, "sensor": "A", "reading": 72, "readingPercentiles": [72, 90] }, { "_id": 2, "sensor": "A", "reading": 85, "readingPercentiles": [72, 90] }, { "_id": 3, "sensor": "A", "reading": 90, "readingPercentiles": [72, 90] }, { "_id": 4, "sensor": "A", "reading": 68, "readingPercentiles": [72, 90] }, { "_id": 5, "sensor": "A", "reading": 95, "readingPercentiles": [72, 90] }, { "_id": 6, "sensor": "B", "reading": 80, "readingPercentiles": [75, 88] }, { "_id": 7, "sensor": "B", "reading": 75, "readingPercentiles": [75, 88] }, { "_id": 8, "sensor": "B", "reading": 92, "readingPercentiles": [75, 88] }, { "_id": 9, "sensor": "B", "reading": 88, "readingPercentiles": [75, 88] }, { "_id": 10, "sensor": "B", "reading": 70, "readingPercentiles": [75, 88] } ]

Each document is augmented with readingPercentiles, the approximate 25th and 75th percentiles of reading across all documents in its partition.

Code examples

To view a code example for using the $percentile operator, choose the tab for the language that you want to use. The following examples show accumulator usage (in $group), expression usage (in $project), and window operator usage (in $setWindowFields):

Node.js
const { MongoClient } = require('mongodb'); async function example() { const uri = 'mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'; const client = new MongoClient(uri); try { await client.connect(); const db = client.db('test'); // Accumulator usage: percentiles across grouped documents const students = db.collection('students'); await students.insertMany([ { class: "A", score: 72 }, { class: "A", score: 85 }, { class: "A", score: 90 }, { class: "A", score: 68 }, { class: "A", score: 95 }, { class: "B", score: 80 }, { class: "B", score: 75 }, { class: "B", score: 92 }, { class: "B", score: 88 }, { class: "B", score: 70 } ]); const accumulatorResult = await students.aggregate([ { $group: { _id: "$class", percentiles: { $percentile: { input: "$score", p: [0.25, 0.75], method: "approximate" } } }} ]).toArray(); console.log('Accumulator result:', accumulatorResult); // Expression usage: percentiles of an array field const surveys = db.collection('surveys'); await surveys.insertMany([ { _id: 1, responses: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] }, { _id: 2, responses: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] } ]); const expressionResult = await surveys.aggregate([ { $project: { quartiles: { $percentile: { input: "$responses", p: [0.25, 0.5, 0.75], method: "approximate" } } }} ]).toArray(); console.log('Expression result:', expressionResult); // Window operator usage: percentiles across each partition const sensorData = db.collection('sensorData'); await sensorData.insertMany([ { _id: 1, sensor: "A", reading: 72 }, { _id: 2, sensor: "A", reading: 85 }, { _id: 3, sensor: "A", reading: 90 }, { _id: 4, sensor: "A", reading: 68 }, { _id: 5, sensor: "A", reading: 95 }, { _id: 6, sensor: "B", reading: 80 }, { _id: 7, sensor: "B", reading: 75 }, { _id: 8, sensor: "B", reading: 92 }, { _id: 9, sensor: "B", reading: 88 }, { _id: 10, sensor: "B", reading: 70 } ]); const windowResult = await sensorData.aggregate([ { $setWindowFields: { partitionBy: "$sensor", output: { readingPercentiles: { $percentile: { input: "$reading", p: [0.25, 0.75], method: "approximate" }, window: { documents: ["unbounded", "unbounded"] } } } } } ]).toArray(); console.log('Window result:', windowResult); } 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'] # Accumulator usage: percentiles across grouped documents students = db['students'] students.insert_many([ { 'class': 'A', 'score': 72 }, { 'class': 'A', 'score': 85 }, { 'class': 'A', 'score': 90 }, { 'class': 'A', 'score': 68 }, { 'class': 'A', 'score': 95 }, { 'class': 'B', 'score': 80 }, { 'class': 'B', 'score': 75 }, { 'class': 'B', 'score': 92 }, { 'class': 'B', 'score': 88 }, { 'class': 'B', 'score': 70 } ]) accumulator_result = list(students.aggregate([ { '$group': { '_id': '$class', 'percentiles': { '$percentile': { 'input': '$score', 'p': [0.25, 0.75], 'method': 'approximate' } } }} ])) print('Accumulator result:', accumulator_result) # Expression usage: percentiles of an array field surveys = db['surveys'] surveys.insert_many([ { '_id': 1, 'responses': [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] }, { '_id': 2, 'responses': [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] } ]) expression_result = list(surveys.aggregate([ { '$project': { 'quartiles': { '$percentile': { 'input': '$responses', 'p': [0.25, 0.5, 0.75], 'method': 'approximate' } } }} ])) print('Expression result:', expression_result) # Window operator usage: percentiles across each partition sensor_data = db['sensorData'] sensor_data.insert_many([ { '_id': 1, 'sensor': 'A', 'reading': 72 }, { '_id': 2, 'sensor': 'A', 'reading': 85 }, { '_id': 3, 'sensor': 'A', 'reading': 90 }, { '_id': 4, 'sensor': 'A', 'reading': 68 }, { '_id': 5, 'sensor': 'A', 'reading': 95 }, { '_id': 6, 'sensor': 'B', 'reading': 80 }, { '_id': 7, 'sensor': 'B', 'reading': 75 }, { '_id': 8, 'sensor': 'B', 'reading': 92 }, { '_id': 9, 'sensor': 'B', 'reading': 88 }, { '_id': 10, 'sensor': 'B', 'reading': 70 } ]) window_result = list(sensor_data.aggregate([ { '$setWindowFields': { 'partitionBy': '$sensor', 'output': { 'readingPercentiles': { '$percentile': { 'input': '$reading', 'p': [0.25, 0.75], 'method': 'approximate' }, 'window': { 'documents': ['unbounded', 'unbounded'] } } } } } ])) print('Window result:', window_result) finally: client.close() example()