$minN (aggregation accumulator)
On this page本页内容
Definition定义
Syntax语法
{
$minN:
{
input: <expression>,
n: <expression>
}
}
input
specifies an expression that is the input to指定一个表达式,该表达式是$minN
.$minN
的输入。It is evaluated for each element in the group and它是为组中的每个元素计算的,$minN
preserves the minimumn
values.$minN
保留最小n
个值。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
.n
限制了每个组的结果数,并且n
必须是一个正整数表达式,该表达式要么是常数,要么取决于$group
的_id
值。
Behavior行为
Null and Missing ValuesNull
值和缺失值
$minN
filters out null and missing values.筛选掉空值和丢失的值。
Consider the following aggregation that returns the minimum 考虑以下聚合,该聚合从一个组中返回最少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",
minimumThreeScores:
{
$minN:
{
input: "$score",
n: 4
}
}
}
}
] )
In this example:在本例中:
$documents
creates the literal documents that contain player scores.创建包含玩家分数的文字文档。$group
groups the documents by按gameId
.gameId
对文档进行分组。This example has only one这个例子只有一个gameId
,G1
.gameId
,G1
。PlayerD
has a missing score and缺少分数,PlayerE
has a nullscore
.PlayerE
的score
为空。These values are both ignored.这两个值都被忽略。TheminimumThreeScores
field is specified as$minN
withinput : "$score"
and returned as an array.minimumThreeScores
字段指定为$minN
,使用input : "$score"
,并以数组的形式返回。Since there are only 3 documents with由于只有3个文档具有scores
minN
returns the minimum 3score
fields even thoughn = 4
.scores
,所以minN
返回最小的3个score
字段,即使n=4
。
[
{
_id: 'G1',
minimumThreeScores: [ 1, 2, 3 ]
}
]
Comparison of $minN
and $bottomN
Accumulators$minN
和$bottomN
累加器的比较
$minN
and $bottomN
AccumulatorsBoth $minN
and $bottomN
accumulators can accomplish similar results.$minN
和$bottomN
累加器都可以实现类似的结果。
In general:一般而言:
$minN
has the advantage of finding minimum values in no particular sort order. If you want to know the minimum values for具有在没有特定排序顺序的情况下找到最小值的优点。如果您想知道n
documents use$minN
.n
个文档的最小值,请使用$minN
。If guaranteing a particular sort order is a requirement use如果需要保证特定的排序顺序,请使用$bottomN
.$bottomN
。Use如果您不打算对输出值进行排序,请使用$bottomN
if you don't intend on sorting on the output values.$bottomN
。
Restrictions限制
Window Function and Aggregation Expression Support窗口函数和聚合表达式支持
You can use 您可以使用$minN
as an accumulator.$minN
作为累加器。
$minN
is supported as an aggregation expression.$minN
被支持作为聚合表达式。
$minN
is supported as a window operator
.$minN
被支持作为窗口运算符。
Memory Limit Considerations内存限制注意事项
Aggregation pipelines which call 调用$minN
are subject to the 100 MB limit. $minN
的聚合管道受100MB限制。If this limit is exceeded for an individual group, the aggregation fails with an error.如果单个组超过此限制,则聚合将失败并出现错误。
Examples实例
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 }
])
Find the Minimum Three Scores
for a Single Game找出单场比赛的最低三分
Scores
for a Single GameYou can use the 您可以使用$minN
accumulator to find the minimum three scores in a single game.$minN
累加器来查找单场比赛中的最低三分。
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
.$match
筛选单个gameId
的结果。在这种情况下,G1
。Uses使用$group
to group the results bygameId
.$group
按gameId
对结果进行分组。In this case,在这种情况下,G1
.G1
。Specifies the fields that are input for指定为$minN
withinput : ["$score","$playerId"]
.$minN
输入的字段,使用input : ["$score","$playerId"]
。Uses使用$minN
to return the first three score elements for theG1
game withn : 3
.$minN
返回n:3
的G1
游戏的前三个分数元素。
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.$minN
累加器来查找每个游戏中的最小n
分。
db.gamescores.aggregate( [
{
$group:
{
_id: "$gameId",
minScores:
{
$minN:
{
input: ["$score","$playerId"],
n: 3
}
}
}
}
] )
The example pipeline:示例管道:
Uses使用$group
to group the results bygameId
.$group
按gameId
对结果进行分组。Uses使用$minN
to return the minimum three score elements for each game withn: 3
.$minN
为n:3
的每个游戏返回至少三个分数元素。Specifies the fields that are input for指定为$minN
withinput: ["$score","$playerId"]
.$minN
输入的字段,使用input: ["$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
基于$Group
的组键计算n
n
Based on the Group Key for $group
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:
{
$minN:
{
input: ["$score","$playerId"],
n: { $cond: { if: {$eq: ["$gameId","G2"] }, then: 1, else: 3 } }
}
}
}
}
] )
The example pipeline:示例管道:
Uses使用$group
to group the results bygameId
.$group
按gameId
对结果进行分组。Specifies the fields that input for指定$minN
withinput : ["$score","$playerId"]
.$minN
的输入字段,使用input : ["$score","$playerId"]
。If the如果gameId
isG2
thenn
is 1, otherwisen
is 3.gameId
是G2
,则n
是1,否则n
是3。
The operation returns the following results:该操作返回以下结果:
[
{ _id: { gameId: 'G2' }, gamescores: [ [ 10, 'PlayerA' ] ] },
{
_id: { gameId: 'G1' },
gamescores: [ [ 1, 'PlayerD' ], [ 31, 'PlayerA' ], [ 33, 'PlayerB' ] ]
}
]