For AI agents: a documentation index is available at https://www.mongodb.com/ko-kr/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
See how MongoDB 9.0 delivers up to 2x higher throughput.
MongoDB Branding Shape
Register now >
Docs Menu

$maxN (expression operator)

$maxN

Returns the n largest values in an array.

Note

Other Uses of $maxN

This page describes $maxN when used as an expression, which returns the largest n values from an array.

You can also use $maxN in these other contexts:

{ $maxN: { n: <expression>, input: <expression> } }
Field
Type
Description

n

Expression

Specifies the number of elements to return. Must resolve to a positive integer.

input

Expression

Specifies the input array from which to return the maximum n elements.

  • $maxN filters out null values.

  • If the input array contains fewer than n elements, $maxN returns all elements in the array.

  • If input resolves to a non-array value, the operation returns an error.

  • If input contains both numeric and string elements, string elements are sorted before numeric elements according to the BSON comparison order.

The examples on this page use data from the sample_mflix dataset. For details on how to load this dataset into your self-managed MongoDB deployment, see Load the Sample Dataset. If you made any modifications to the sample databases, you may need to drop and recreate the databases to run the examples on this page.

The following example uses $maxN to return the two alphabetically last cast members for each movie, stored in a new topTwoCast field created by $addFields.

db.movies.aggregate( [
{
$match: {
title: { $in: [ "The Godfather", "Spirited Away", "The Dark Knight" ] }
}
},
{
$addFields: {
topTwoCast: { $maxN: { n: 2, input: "$cast" } }
}
},
{
$project: {
_id: 0,
title: 1,
cast: 1,
topTwoCast: 1
}
},
{
$sort: { title: 1 }
}
] )

The preceding pipeline:

  • Uses $match to filter for three specific movies.

  • Uses $addFields with $maxN to create a new field topTwoCast containing the two alphabetically last values from the cast array with n: 2.

  • Uses $project to include only the title, cast, and topTwoCast fields in the output.

  • Uses $sort to sort the results by title.