“文档” 菜单
文档首页
/
MongoDB Manual
/ / /

$minN(聚合累加器)

在此页面上

  • 定义
  • 语法
  • 行为
  • 限制
  • 举例
$minN

5.2 版本中的新增功能

返回群组内 n 个最小值元素的聚合。 如果该组包含的元素少于 n 个,$minN 返回该组中的所有元素。

{
$minN:
{
input: <expression>,
n: <expression>
}
}
  • input 指定一个表达式,作为$minN的输入。 系统会针对群组中的每个元素进行评估,而$minN会保留最小n值。

  • n 限制每组的结果数量,并且n必须是正整数表达式,它可以是常量或取决于 $group_id值。

  • $minN 过滤掉空值和缺失值。

考虑以下聚合,该聚合从组中返回最少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
}
}
}
}
] )

在本例中:

  • $documents 创建包含玩家分数的字面文档。

  • $groupgameId 对文档进行分组。此示例只有一个 gameIdG1

  • PlayerD 分数缺失,且PlayerEscore为空。 这些值都被忽略。

  • minimumThreeScores字段指定为带有input : "$score" $minN并作为数组返回。

  • 由于只有 3 个具有 的文档,因此即使scoresminN scoren = 4, 也会返回最少 3 个字段。

[
{
_id: 'G1',
minimumThreeScores: [ 1, 2, 3 ]
}
]

$minN$bottomN累加器都可以实现类似的结果。

一般来说:

  • $minN的优点是查找最小值时无需特定的排序顺序。 如果您想知道n文档的最小值,请使用$minN

  • 如果要求保证特定的排序顺序,请使用$bottomN

  • 如果不打算对输出值进行排序,请使用$bottomN

您可以将$minN用作累加器。

支持将$minN作为聚合表达式。

支持将$minN作为window operator

调用 $minN 的聚合管道受 100 MB 限制。如果单个群组超过此限制,则聚合失败,并显示错误。

请考虑包含以下文档的 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 }
])

您可以使用$minN累加器查找单个游戏中的最小三个分数。

db.gamescores.aggregate( [
{
$match : { gameId : "G1" }
},
{
$group:
{
_id: "$gameId",
minScores:
{
$minN:
{
input: ["$score","$playerId"],
n:3
}
}
}
}
] )

示例管道:

  • 使用 $match 筛选单个 gameId 的结果。在本例中为 G1

  • 使用 $groupgameId 对结果分组。本例中为 G1

  • 指定使用input : ["$score","$playerId"]$minN输入的字段。

  • 使用$minN返回具有n : 3G1游戏的前三个分数元素。

操作返回以下结果:

[
{
_id: 'G1',
minScores: [ [ 1, 'PlayerD' ], [ 31, 'PlayerA' ], [ 33, 'PlayerB' ] ]
}
]

您可以使用$minN累加器查找每场游戏中的最低n分数。

db.gamescores.aggregate( [
{
$group:
{
_id: "$gameId",
minScores:
{
$minN:
{
input: ["$score","$playerId"],
n: 3
}
}
}
}
] )

示例管道:

  • 使用 $groupgameId 对结果进行分组。

  • 使用$minN返回每个具有n: 3的游戏的最小三个分数元素。

  • 指定使用input: ["$score","$playerId"]$minN输入的字段。

操作返回以下结果:

[
{
_id: 'G2',
minScores: [ [ 10, 'PlayerA' ], [ 14, 'PlayerB' ], [ 66, 'PlayerC' ] ]
},
{
_id: 'G1',
minScores: [ [ 1, 'PlayerD' ], [ 31, 'PlayerA' ], [ 33, 'PlayerB' ] ]
}
]

您还可以动态分配n的值。 在此示例中,对gameId字段使用了$cond表达式。

db.gamescores.aggregate([
{
$group:
{
_id: {"gameId": "$gameId"},
gamescores:
{
$minN:
{
input: ["$score","$playerId"],
n: { $cond: { if: {$eq: ["$gameId","G2"] }, then: 1, else: 3 } }
}
}
}
}
] )

示例管道:

  • 使用 $groupgameId 对结果进行分组。

  • 指定使用input : ["$score","$playerId"]输入$minN的字段。

  • 如果gameIdG2 ,则n为 1,否则n为 3。

操作返回以下结果:

[
{ _id: { gameId: 'G2' }, gamescores: [ [ 10, 'PlayerA' ] ] },
{
_id: { gameId: 'G1' },
gamescores: [ [ 1, 'PlayerD' ], [ 31, 'PlayerA' ], [ 33, 'PlayerB' ] ]
}
]

后退

$min(聚合)

来年

$minN(数组运算符)