View a markdown version of this page

$max - Amazon DocumentDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

$max

The $max aggregation operator returns the maximum value of a specified field across a set of documents. This operator is useful for finding the highest value in a set of documents.

Parameters

  • expression: The expression to use to calculate the maximum value.

Example (MongoDB Shell)

The following example demonstrates how to use the $max operator to find the maximum score in a collection of student documents. The $group stage groups all documents together, and the $max operator is used to calculate the maximum value of the score field across all documents.

Create sample documents

db.students.insertMany([ { name: "John", score: 85 }, { name: "Jane", score: 92 }, { name: "Bob", score: 78 }, { name: "Alice", score: 90 } ])

Query example

db.students.aggregate([ { $group: { _id: null, maxScore: { $max: "$score" } } }, { $project: { _id: 0, maxScore: 1 } } ])

Output

[ { maxScore: 92 } ]

Window operator usage example (MongoDB Shell)

New from version 8.0.2.

The $max operator can also be used as a window operator in the $setWindowFields stage. In this context, it returns the maximum value 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, $max is limited to 100 MB of intermediate data. An operation that exceeds this limit returns an error.

Create sample documents

db.stockPrices.insertMany([ { _id: 1, ticker: "ABC", hour: 1, price: 50 }, { _id: 2, ticker: "ABC", hour: 2, price: 45 }, { _id: 3, ticker: "ABC", hour: 3, price: 60 }, { _id: 4, ticker: "ABC", hour: 4, price: 40 }, { _id: 5, ticker: "XYZ", hour: 1, price: 30 }, { _id: 6, ticker: "XYZ", hour: 2, price: 35 }, { _id: 7, ticker: "XYZ", hour: 3, price: 25 } ]);

Query example

The following example partitions the documents by ticker, sorts each partition by hour, and returns the highest price seen from the start of the partition through the current document.

db.stockPrices.aggregate([ { $setWindowFields: { partitionBy: "$ticker", sortBy: { hour: 1 }, output: { highestSoFar: { $max: "$price", window: { documents: ["unbounded", "current"] } } } } } ]);

Output

[ { "_id": 1, "ticker": "ABC", "hour": 1, "price": 50, "highestSoFar": 50 }, { "_id": 2, "ticker": "ABC", "hour": 2, "price": 45, "highestSoFar": 50 }, { "_id": 3, "ticker": "ABC", "hour": 3, "price": 60, "highestSoFar": 60 }, { "_id": 4, "ticker": "ABC", "hour": 4, "price": 40, "highestSoFar": 60 }, { "_id": 5, "ticker": "XYZ", "hour": 1, "price": 30, "highestSoFar": 30 }, { "_id": 6, "ticker": "XYZ", "hour": 2, "price": 35, "highestSoFar": 35 }, { "_id": 7, "ticker": "XYZ", "hour": 3, "price": 25, "highestSoFar": 35 } ]

Each document is augmented with highestSoFar, the running maximum of price within its partition up to and including the current document.

Code examples

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

Node.js
const { MongoClient } = require('mongodb'); async function example() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); // Accumulator usage: maximum value across a group const students = db.collection('students'); await students.insertMany([ { name: "John", score: 85 }, { name: "Jane", score: 92 }, { name: "Bob", score: 78 }, { name: "Alice", score: 90 } ]); const accumulatorResult = await students.aggregate([ { $group: { _id: null, maxScore: { $max: "$score" } } } ]).toArray(); console.log('Accumulator result:', accumulatorResult); // Window operator usage: running maximum within each partition const stockPrices = db.collection('stockPrices'); await stockPrices.insertMany([ { _id: 1, ticker: "ABC", hour: 1, price: 50 }, { _id: 2, ticker: "ABC", hour: 2, price: 45 }, { _id: 3, ticker: "ABC", hour: 3, price: 60 }, { _id: 4, ticker: "ABC", hour: 4, price: 40 }, { _id: 5, ticker: "XYZ", hour: 1, price: 30 }, { _id: 6, ticker: "XYZ", hour: 2, price: 35 }, { _id: 7, ticker: "XYZ", hour: 3, price: 25 } ]); const windowResult = await stockPrices.aggregate([ { $setWindowFields: { partitionBy: "$ticker", sortBy: { hour: 1 }, output: { highestSoFar: { $max: "$price", window: { documents: ["unbounded", "current"] } } } } } ]).toArray(); console.log('Window result:', windowResult); 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') db = client['test'] # Accumulator usage: maximum value across a group students = db.students students.insert_many([ { "name": "John", "score": 85 }, { "name": "Jane", "score": 92 }, { "name": "Bob", "score": 78 }, { "name": "Alice", "score": 90 } ]) accumulator_result = list(students.aggregate([ { "$group": { "_id": None, "maxScore": { "$max": "$score" } } } ])) print('Accumulator result:', accumulator_result) # Window operator usage: running maximum within each partition stock_prices = db.stockPrices stock_prices.insert_many([ { "_id": 1, "ticker": "ABC", "hour": 1, "price": 50 }, { "_id": 2, "ticker": "ABC", "hour": 2, "price": 45 }, { "_id": 3, "ticker": "ABC", "hour": 3, "price": 60 }, { "_id": 4, "ticker": "ABC", "hour": 4, "price": 40 }, { "_id": 5, "ticker": "XYZ", "hour": 1, "price": 30 }, { "_id": 6, "ticker": "XYZ", "hour": 2, "price": 35 }, { "_id": 7, "ticker": "XYZ", "hour": 3, "price": 25 } ]) window_result = list(stock_prices.aggregate([ { "$setWindowFields": { "partitionBy": "$ticker", "sortBy": { "hour": 1 }, "output": { "highestSoFar": { "$max": "$price", "window": { "documents": ["unbounded", "current"] } } } } } ])) print('Window result:', window_result) client.close() example()