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

$maxN(聚合累加器)

在此页面上

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

5.2 版本中的新增功能

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

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

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

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

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

在本例中:

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

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

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

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

  • 由于只有 3 个包含scoresmaxN score的文档,因此即使n = 4 , 也会返回最大 3 个字段。

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

$maxN$topN累加器都可以实现类似的结果。

一般来说:

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

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

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

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

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

支持将$maxN作为window operator

调用 $maxN 的聚合管道受 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 }
])

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

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

示例管道:

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

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

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

  • 使用$maxN返回n : 3G1游戏的最大三个分数元素。

操作返回以下结果:

[
{
_id: 'G1',
maxThreeScores: [ [ 99, 'PlayerC' ], [ 33, 'PlayerB' ], [ 31, 'PlayerA' ] ]
}
]

您可以使用$maxN累加器查找每个游戏中的最大n分数。

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

示例管道:

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

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

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

操作返回以下结果:

[
{
_id: 'G1',
maxScores: [ [ 99, 'PlayerC' ], [ 33, 'PlayerB' ], [ 31, 'PlayerA' ] ]
},
{
_id: 'G2',
maxScores: [ [ 80, 'PlayerD' ], [ 66, 'PlayerC' ], [ 14, 'PlayerB' ] ]
}
]

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

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

示例管道:

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

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

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

操作返回以下结果:

[
{ _id: { gameId: 'G2' }, gamescores: [ [ 80, 'PlayerD' ] ] },
{
_id: { gameId: 'G1' },
gamescores: [ [ 99, 'PlayerC' ], [ 33, 'PlayerB' ], [ 31, 'PlayerA' ] ]
}
]
← $max(聚合)