Docs Home → Develop Applications → MongoDB Manual
$maxN (aggregation accumulator)![](/docs/v5.3/assets/link.svg)
On this page
Definition![](/docs/v5.3/assets/link.svg)
Syntax![](/docs/v5.3/assets/link.svg)
{ $maxN: { input: <expression>, n: <expression> } }
input
specifies an expression that is the input to$maxN
. It is evaluated for each element in the group and$maxN
preserves the maximumn
values.n
limits the number of results per group andn
has to be a positive integral expression that is either a constant or depends on the_id
value for$group
.
Behavior![](/docs/v5.3/assets/link.svg)
Null and Missing Values![](/docs/v5.3/assets/link.svg)
$maxN
filters out null and missing values.
Consider the following aggregation that returns the maximum n
documents from a group:
db.aggregate( [ { $documents: [ { playerId: "PlayerA", gameId: "G1", score: 1 }, { playerId: "PlayerB", gameId: "G1", score: 2 }, { playerId: "PlayerC", gameId: "G1", score: 3 }, { playerId: "PlayerD", gameId: "G1" }, { playerId: "PlayerE", gameId: "G1", score: null } ] }, { $group: { _id: "$gameId", maximumThreeScores: { $maxN: { input: "$score", n: 4 } } } } ] )
In this example:
$documents
creates the literal documents that contain player scores.$group
groups the documents bygameId
. This example has only onegameId
,G1
.PlayerD
has a missing score andPlayerE
has a nullscore
. These values are both considered as null.The
maximumThreeScores
field is specified as$maxN
withinput : "$score"
and returned as an array.Since there are only 3 documents with
scores
maxN
returns the maximum 3score
fields even thoughn = 4
.
[ { _id: 'G1', maximumThreeScores: [ 3, 2, 1 ] } ]
Comparison of $maxN
and $topN
Accumulators![](/docs/v5.3/assets/link.svg)
Both $maxN
and $topN
accumulators can accomplish similar
results.
In general:
Restrictions![](/docs/v5.3/assets/link.svg)
Window Function and Aggregation Expression Support![](/docs/v5.3/assets/link.svg)
You can use $maxN
as an accumulator.
$maxN
is supported as an
aggregation expression.
$maxN
is supported as a
window operator
.
Memory Limit Considerations![](/docs/v5.3/assets/link.svg)
Aggregation pipelines which call $maxN
are subject to the
100 MB limit. If this
limit is exceeded for an individual group, the aggregation fails
with an error.
Examples![](/docs/v5.3/assets/link.svg)
Consider a gamescores
collection with the following documents:
db.gamescores.insertMany([ { playerId: "PlayerA", gameId: "G1", score: 31 }, { playerId: "PlayerB", gameId: "G1", score: 33 }, { playerId: "PlayerC", gameId: "G1", score: 99 }, { playerId: "PlayerD", gameId: "G1", score: 1 }, { playerId: "PlayerA", gameId: "G2", score: 10 }, { playerId: "PlayerB", gameId: "G2", score: 14 }, { playerId: "PlayerC", gameId: "G2", score: 66 }, { playerId: "PlayerD", gameId: "G2", score: 80 } ])
Find the Maximum Three Scores
for a Single Game![](/docs/v5.3/assets/link.svg)
You can use the $maxN
accumulator to find the maximum three scores
in a single game.
db.gamescores.aggregate( [ { $match : { gameId : "G1" } }, { $group: { _id: "$gameId", maxThreeScores: { $maxN: { input: ["$score","$playerId"], n:3 } } } } ] )
The example pipeline:
Uses
$match
to filter the results on a singlegameId
. In this case,G1
.Uses
$group
to group the results bygameId
. In this case,G1
.Specifies the fields that are input for
$maxN
withinput : ["$score","$playerId"]
.Uses
$maxN
to return the maximum three score elements for theG1
game withn : 3
.
The operation returns the following results:
[ { _id: 'G1', maxThreeScores: [ [ 99, 'PlayerC' ], [ 33, 'PlayerB' ], [ 31, 'PlayerA' ] ] } ]
Finding the Maximum Three Scores Across Multiple Games![](/docs/v5.3/assets/link.svg)
You can use the $maxN
accumulator to find the maximum n
scores in each game.
db.gamescores.aggregate( [ { $group: { _id: "$gameId", maxScores: { $maxN: { input: ["$score","$playerId"], n: 3 } } } } ] )
The example pipeline:
Uses
$group
to group the results bygameId
.Uses
$maxN
to return the maximum three score elements for each game withn: 3
.Specifies the fields that are input for
$maxN
withinput: ["$score","$playerId"]
.
The operation returns the following results:
[ { _id: 'G1', maxScores: [ [ 99, 'PlayerC' ], [ 33, 'PlayerB' ], [ 31, 'PlayerA' ] ] }, { _id: 'G2', maxScores: [ [ 80, 'PlayerD' ], [ 66, 'PlayerC' ], [ 14, 'PlayerB' ] ] } ]
Computing n
Based on the Group Key for $group
![](/docs/v5.3/assets/link.svg)
You can also assign the value of n
dynamically. In this example,
the $cond
expression is used on the gameId
field.
db.gamescores.aggregate([ { $group: { _id: {"gameId": "$gameId"}, gamescores: { $maxN: { input: ["$score","$playerId"], n: { $cond: { if: {$eq: ["$gameId","G2"] }, then: 1, else: 3 } } } } } } ] )
The example pipeline:
Uses
$group
to group the results bygameId
.Specifies the fields that input for
$maxN
withinput : ["$score","$playerId"]
.If the
gameId
isG2
thenn
is 1, otherwisen
is 3.
The operation returns the following results:
[ { _id: { gameId: 'G2' }, gamescores: [ [ 80, 'PlayerD' ] ] }, { _id: { gameId: 'G1' }, gamescores: [ [ 99, 'PlayerC' ], [ 33, 'PlayerB' ], [ 31, 'PlayerA' ] ] } ]