Definition定义
$minNNew in version 5.2.在版本5.2中新增。Returns an aggregation of the minimum value返回组内最小值nelements within a group. If the group contains fewer thannelements,$minNreturns all elements in the group.n个元素的聚合。如果组包含的元素少于n个,$minN将返回组中的所有元素。
Syntax语法
{
$minN:
{
input: <expression>,
n: <expression>
}
}
inputspecifies an expression that is the input to指定作为$minN. It is evaluated for each element in the group and$minNpreserves the minimumnvalues.$minN输入的表达式。对组中的每个元素进行评估,$minN保留最小n值。nlimits the number of results per group and限制每组的结果数量,nhas to be a positive integral expression that is either a constant or depends on the_idvalue for$group.n必须是一个正积分表达式,要么是常数,要么取决于$group的_id值。
Behavior行为
Result Type结果类型
$minN compares input data following the BSON Comparison order to determine the appropriate output type. When the input data contains multiple data types, the $minN output type is the lowest in the comparison order.$minN按照BSON比较顺序比较输入数据,以确定适当的输出类型。当输入数据包含多种数据类型时,$minN输出类型在比较顺序中最低。
Null and Missing Valuesnull值和缺失值
$minNfilters out null and missing values.筛选掉null值和缺失值。
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:在这个例子中:
$documentscreates the literal documents that contain player scores.创建包含玩家分数的文字文档。$groupgroups the documents by按gameId. This example has only onegameId,G1.gameId对文档进行分组。这个例子只有一个gameId,即G1。PlayerDhas a missing score and缺少PlayerEhas a nullscore. These values are both ignored.score,而PlayerE的score为空。这两个值都被忽略了。TheminimumThreeScoresfield is specified as$minNwithinput : "$score"and returned as an array.minimumThreeScores字段被指定为$minN配合input : "$score",并作为数组返回。Since there are only 3 documents with由于只有3个文档具有scoresminNreturns the minimum 3scorefields even thoughn = 4.scores,即使n=4,minN也会返回至少3个分数字段。
[
{
_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:一般来说:
$minNhas the advantage of finding minimum values in no particular sort order. If you want to know the minimum values for具有在没有特定排序顺序的情况下找到最小值的优点。如果你想知道ndocuments use$minN.n个文档的最小值,请使用$minN。If guaranteing a particular sort order is a requirement use如果需要保证特定的排序顺序,请使用$bottomN.$bottomN。Use如果您不打算对输出值进行排序,请使用$bottomNif 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. If this limit is exceeded for an individual group, the aggregation fails with an error.$minN的聚合管道受100MB限制。如果单个组超过此限制,聚合将失败并出现错误。
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使用$matchto filter the results on a singlegameId. In this case,G1.$match筛选单个gameId的结果。在这种情况下,G1。Uses使用$groupto group the results bygameId. In this case,G1.$group按gameId对结果进行分组。在这种情况下,G1。Specifies the fields that are input for使用$minNwithinput : ["$score","$playerId"].input : ["$score","$playerId"]指定$minN的输入字段。Uses使用$minNto return the first three score elements for theG1game 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使用$groupto group the results bygameId.$group按gameId对结果进行分组。Uses使用$minNto return the minimum three score elements for each game withn: 3.$minN配合n:3返回每个游戏的最小三个分数元素。Specifies the fields that are input for使用$minNwithinput: ["$score","$playerId"].input: ["$score","$playerId"]指定$minN的输入字段。
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 $groupYou can also assign the value of 您还可以动态地分配n dynamically. In this example, the $cond expression is used on the gameId field.n的值。在这个例子中,$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使用$groupto group the results bygameId.$group按gameId对结果进行分组。Specifies the fields that input for使用$minNwithinput : ["$score","$playerId"].input : ["$score","$playerId"]为$minN指定输入的字段。If the如果gameIdisG2thennis 1, otherwisenis 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' ] ]
}
]