$max
The $max aggregation stage is used to return the maximum value of a specified field across all documents in a pipeline stage. 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 } ]
Code examples
To view a code example for using the $max command, choose the tab for the language that you want to use: