On this page本页内容
{
$minN:
{
input: <expression>,
n: <expression>
}
}
input$minN. $minN输入的表达式。$minN preserves the minimum n values.$minN保留最小n个值。nn has to be a positive integral expression that is either a constant or depends on the _id value for $group.n必须是一个正整数表达式,它要么是常数,要么取决于$group的_id值。Null和缺少值$minNConsider 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$groupgameId. gameId对文档进行分组。gameId, G1.gameId,G1。PlayerDPlayerE has a null score. score,而PlayerE的score为空。minimumThreeScores field is specified as $minN with input : "$score" and returned as an array.minimumThreeScores字段使用input : "$score"指定为$minN,并作为数组返回。scores minN returns the minimum 3 score fields even though n = 4.scores,因此即使n=4,minN也会返回最少3个score字段。[
{
_id: 'G1',
minimumThreeScores: [ 1, 2, 3 ]
}
]
$minN and $bottomN Accumulators$minN和$bottomN累加器的比较Both $minN and $bottomN accumulators can accomplish similar results.$minN和$bottomN累加器都可以实现类似的结果。
In general:
$minNn documents use $minN.n个文档的最小值,请使用$minN。$bottomN.$bottomN。$bottomN if you don't intend on sorting on the output values.$bottomN。You can use 您可以使用$minN as an accumulator.$minN作为累加器。
$minN is supported as an aggregation expression.支持作为聚合表达式。
$minN is supported as a window operator.支持作为窗口运算符。
Aggregation pipelines which call 调用$minN are subject to the 100 MB limit. $minN的聚合管道受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 您可以使用$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:示例管道:
$match to filter the results on a single gameId. $match筛选单个gameId上的结果。G1.G1。$group to group the results by gameId. $group按gameId对结果进行分组。G1.G1。$minN with input : ["$score","$playerId"].input : ["$score","$playerId"]指定$minN的输入字段。$minN to return the first three score elements for the G1 game with n : 3.$minN返回n:3的G1游戏的前三个分数元素。The operation returns the following results:该操作返回以下结果:
[
{
_id: 'G1',
minScores: [ [ 1, 'PlayerD' ], [ 31, 'PlayerA' ], [ 33, 'PlayerB' ] ]
}
]
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:示例管道:
$group to group the results by gameId.$group按gameId对结果进行分组。$minN to return the minimum three score elements for each game with n: 3.$minN返回n:3的每个游戏的最少三个分数元素。$minN with input: ["$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' ] ]
}
]
n Based on the Group Key for $group$Group的组密钥计算nYou 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:示例管道:
$group to group the results by gameId.$group按gameId对结果进行分组。$minN with input : ["$score","$playerId"].input : ["$score","$playerId"]为$minN指定输入的字段。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: [ [ 10, 'PlayerA' ] ] },
{
_id: { gameId: 'G1' },
gamescores: [ [ 1, 'PlayerD' ], [ 31, 'PlayerA' ], [ 33, 'PlayerB' ] ]
}
]