$minN (aggregation accumulator)
On this page
Definition
Syntax
{ $minN: { input: <expression>, n: <expression> } }
input
specifies an expression that is the input to$minN
. It is evaluated for each element in the group and$minN
preserves the minimumn
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
Null and Missing Values
$minN
filters out null and missing values.
Consider the following aggregation that returns the minimum 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", minimumThreeScores: { $minN: { 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 ignored.The
minimumThreeScores
field is specified as$minN
withinput : "$score"
and returned as an array.Since there are only 3 documents with
scores
minN
returns the minimum 3score
fields even thoughn = 4
.
[ { _id: 'G1', minimumThreeScores: [ 1, 2, 3 ] } ]
Comparison of $minN
and $bottomN
Accumulators
Both $minN
and $bottomN
accumulators can accomplish
similar results.
In general:
Restrictions
Window Function and Aggregation Expression Support
You can use $minN
as an accumulator.
$minN
is supported as an
aggregation expression.
$minN
is supported as a
window operator
.
Memory Limit Considerations
Aggregation pipelines which call $minN
are subject to the
100 MB limit. If this
limit is exceeded for an individual group, the aggregation fails
with an error.
Examples
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 Minimum Three Scores
for a Single Game
You can use the $minN
accumulator to find the minimum three scores
in a single game.
db.gamescores.aggregate( [ { $match : { gameId : "G1" } }, { $group: { _id: "$gameId", minScores: { $minN: { 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
$minN
withinput : ["$score","$playerId"]
.Uses
$minN
to return the first three score elements for theG1
game withn : 3
.
The operation returns the following results:
[ { _id: 'G1', minScores: [ [ 1, 'PlayerD' ], [ 31, 'PlayerA' ], [ 33, 'PlayerB' ] ] } ]
Finding the Minimum Three Documents Across Multiple Games
You can use the $minN
accumulator to find the minimum n
scores in each game.
db.gamescores.aggregate( [ { $group: { _id: "$gameId", minScores: { $minN: { input: ["$score","$playerId"], n: 3 } } } } ] )
The example pipeline:
Uses
$group
to group the results bygameId
.Uses
$minN
to return the minimum three score elements for each game withn: 3
.Specifies the fields that are input for
$minN
withinput: ["$score","$playerId"]
.
The operation returns the following results:
[ { _id: 'G2', minScores: [ [ 10, 'PlayerA' ], [ 14, 'PlayerB' ], [ 66, 'PlayerC' ] ] }, { _id: 'G1', minScores: [ [ 1, 'PlayerD' ], [ 31, 'PlayerA' ], [ 33, 'PlayerB' ] ] } ]
Computing n
Based on the Group Key for $group
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: { $minN: { 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
$minN
withinput : ["$score","$playerId"]
.If the
gameId
isG2
thenn
is 1, otherwisen
is 3.
The operation returns the following results:
[ { _id: { gameId: 'G2' }, gamescores: [ [ 10, 'PlayerA' ] ] }, { _id: { gameId: 'G1' }, gamescores: [ [ 1, 'PlayerD' ], [ 31, 'PlayerA' ], [ 33, 'PlayerB' ] ] } ]