On this page本页内容
{ $maxN: { input: <expression>, n: <expression> } }
input
$maxN
. $maxN
输入的表达式。$maxN
preserves the maximum n
values.$maxN
保留最大n个值。n
n
has to be a positive integral expression that is either a constant or depends on the _id
value for $group
.n
必须是一个正整数表达式,它要么是常数,要么取决于$group
的_id
值。Null
和缺少值$maxN
Consider the following aggregation that returns the maximum 考虑以下聚合,该聚合从一个组中返回最多n
documents from a group:n
个文档:
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
$group
gameId
. gameId
对文档进行分组。gameId
, G1
.gameId
,G1
。PlayerD
PlayerE
has a null score
. score
,而PlayerE
的score
为空。null
。maximumThreeScores
field is specified as $maxN
with input : "$score"
and returned as an array.maximumThreeScores
字段被指定为$maxN
使用input : "$score"
,并作为数组返回。scores
maxN
returns the maximum 3 score
fields even though n = 4
.scores
为maxN
,因此即使n=4
,也会返回最多3个scores
字段。[ { _id: 'G1', maximumThreeScores: [ 3, 2, 1 ] } ]
$maxN
and $topN
Accumulators$maxN
和$topN
累加器的比较Both $maxN
and $topN
accumulators can accomplish similar results.$maxN
和$topN
累加器都可以实现类似的结果。
In general:一般来说:
$maxN
n
documents use $maxN
.n
个文档的最大值,请使用$maxN
。$topN
.$topN
。$topN
if you don't intend on sorting on the output values.$topN
。You can use 您可以使用$maxN
as an accumulator.$maxN
作为累加器。
$maxN
is supported as an aggregation expression.支持作为聚合表达式。
$maxN
is supported as a window operator.支持作为窗口运算符。
Aggregation pipelines which call 调用$maxN
are subject to the 100 MB limit. $maxN
的聚合管道受100 MB限制。If this limit is exceeded for an individual group, the aggregation fails with an error.如果单个组超过了此限制,则聚合将失败并返回错误。
Consider a 考虑一个包含以下文档的gamescores
collection with the following documents:gamescores
集合:
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 } ])
Scores
for a Single GameYou can use the 您可以使用$maxN
accumulator to find the maximum three scores in a single game.$maxN
累加器查找单个游戏中最多三个分数。
db.gamescores.aggregate( [ { $match : { gameId : "G1" } }, { $group: { _id: "$gameId", maxThreeScores: { $maxN: { input: ["$score","$playerId"], n:3 } } } } ] )
The example pipeline:示例管道:
$match
to filter the results on a single gameId
. In this case, G1
.$match
筛选单个gameId
上的结果。在本例中为G1
。$group
to group the results by gameId
. In this case, G1
.$group
按gameId
对结果进行分组。在本例中为G1
。$maxN
with input : ["$score","$playerId"]
.input : ["$score","$playerId"]
指定$maxN
的输入字段。$maxN
to return the maximum three score elements for the G1
game with n : 3
.$maxN
返回n:3
的G1
游戏的最多三个分数元素。The operation returns the following results:该操作返回以下结果:
[ { _id: 'G1', maxThreeScores: [ [ 99, 'PlayerC' ], [ 33, 'PlayerB' ], [ 31, 'PlayerA' ] ] } ]
You can use the 您可以使用$maxN
accumulator to find the maximum n
scores in each game.$maxN
累加器查找每个游戏中的最大n
个分数。
db.gamescores.aggregate( [ { $group: { _id: "$gameId", maxScores: { $maxN: { input: ["$score","$playerId"], n: 3 } } } } ] )
The example pipeline:示例管道:
$group
to group the results by gameId
.$group
按gameId
对结果进行分组。$maxN
to return the maximum three score elements for each game with n: 3
.$maxN
返回n:3
的每个游戏的最多三个分数元素。$maxN
with input: ["$score","$playerId"]
.input: ["$score","$playerId"]
指定$maxN
的输入字段。The operation returns the following results:该操作返回以下结果:
[ { _id: 'G1', maxScores: [ [ 99, 'PlayerC' ], [ 33, 'PlayerB' ], [ 31, 'PlayerA' ] ] }, { _id: 'G2', maxScores: [ [ 80, 'PlayerD' ], [ 66, 'PlayerC' ], [ 14, 'PlayerB' ] ] } ]
n
Based on the Group Key for $group
$Group
的组密钥计算n
You can also assign the value of 还可以动态指定n
dynamically. n
的值。In this example, the 在本例中,$cond
expression is used on the gameId
field.$cond
表达式用于gameId
字段。
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:示例管道:
$group
to group the results by gameId
.$group
按gameId
对结果进行分组。$maxN
with input : ["$score","$playerId"]
.input : ["$score","$playerId"]
为$maxN
输入的字段。gameId
is G2
then n
is 1, otherwise n
is 3.gameId
为G2
,则n
为1,否则n
为3。The operation returns the following results:该操作返回以下结果:
[ { _id: { gameId: 'G2' }, gamescores: [ [ 80, 'PlayerD' ] ] }, { _id: { gameId: 'G1' }, gamescores: [ [ 99, 'PlayerC' ], [ 33, 'PlayerB' ], [ 31, 'PlayerA' ] ] } ]