文档菜单
文档首页
/
MongoDB 手册
/ / /

$bottomN(聚合累加器)

本页内容

  • 定义
  • 语法
  • 行为
  • 限制
  • 示例
$bottomN

新增在版本5.2.

根据指定的排序顺序返回组内底部n个元素的聚合。如果组中元素少于n,则$bottomN返回组中的所有元素。

{
$bottomN:
{
n: <expression>,
sortBy: { <field1>: <sort order>, <field2>: <sort order> ... },
output: <expression>
}
}
  • n限制每组结果的数量,必须是正整数表达式,可以是常量或依赖于$group.

  • sortBy指定结果的顺序,语法类似于$sort.

  • output表示组中每个元素的结果,可以是任何表达式。

  • $bottomN不会过滤掉空值。

  • $bottomN将缺失值转换为空值,这些空值在输出中保留。

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",
playerId:
{
$bottomN:
{
output: [ "$playerId", "$score" ],
sortBy: { "score": -1 },
n: 3
}
}
}
}
] )

在本例中

  • $documents创建包含玩家得分的文档。

  • $groupgameId对文档进行分组。本例中只有一个gameId,即G1

  • PlayerD有一个缺失的分数,而PlayerE有一个空score。这两个值都被视为空值。

  • playerIdscore字段指定为output : ["$playerId", "$score"],并以数组值返回。

  • 由于sortBy: { "score" : -1 },空值被排序到返回的playerId数组末尾。

[
{
_id: "G1",
playerId: [ [ "PlayerA", 1 ], [ "PlayerD", null ], [ "PlayerE", null ] ]
}
]

当排序不同类型时,使用BSON数据类型的顺序来确定排序。以下是一个包含字符串和数字值的集合的例子。

  • 在升序排序中,字符串值在数值值之后排序。

  • 在降序排序中,字符串值在数值值之前排序。

db.aggregate( [
{
$documents: [
{ playerId: "PlayerA", gameId: "G1", score: 1 },
{ playerId: "PlayerB", gameId: "G1", score: "2" },
{ playerId: "PlayerC", gameId: "G1", score: "" }
]
},
{
$group:
{
_id: "$gameId",
playerId: {
$bottomN:
{
output: ["$playerId","$score"],
sortBy: {"score": -1},
n: 3
}
}
}
}
] )

在本例中

  • PlayerA 拥有一个整数分数。

  • PlayerB 拥有一个字符串分数 "2"

  • PlayerC 拥有一个空字符串分数。

因为排序是降序的 { "score" : -1 },所以字符串字面量值在 PlayerA 的数值分数之前排序。

[
{
_id: "G1",
playerId: [ [ "PlayerB", "2" ], [ "PlayerC", "" ], [ "PlayerA", 1 ] ]
}
]

$bottomN 不支持作为 聚合表达式

$bottomN 支持作为 窗口操作符

$bottomN 聚合管道中的分组受到 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 }
])

您可以使用 $bottomN 累加器在一个游戏中找到得分最低的玩家。

db.gamescores.aggregate( [
{
$match : { gameId : "G1" }
},
{
$group:
{
_id: "$gameId",
playerId:
{
$bottomN:
{
output: ["$playerId", "$score"],
sortBy: { "score": -1 },
n:3
}
}
}
}
] )

示例管道

  • 使用 $match 过滤单个 gameId 的结果。在这个例子中,是 G1

  • 使用 $group 按照游戏 gameId 对结果进行分组。在这个例子中,是 G1

  • 使用 { "score": -1 } 对结果进行降序排序。

  • 使用 output : ["$playerId"," $score"] 指定从 $bottomN 输出的字段。

  • 使用 $bottomN 返回最低得分的三个文档,对于 G1 游戏的 n : 3

操作返回以下结果

[
{
_id: "G1",
playerId: [ [ "PlayerB", 33 ], [ "PlayerA", 31 ], [ "PlayerD", 1 ] ]
}
]

此查询的 SQL 等价形式是

SELECT T3.GAMEID,T3.PLAYERID,T3.SCORE
FROM GAMESCORES AS GS
JOIN (SELECT TOP 3
GAMEID,PLAYERID,SCORE
FROM GAMESCORES
WHERE GAMEID = "G1"
ORDER BY SCORE) AS T3
ON GS.GAMEID = T3.GAMEID
GROUP BY T3.GAMEID,T3.PLAYERID,T3.SCORE
ORDER BY T3.SCORE DESC

您可以使用 $bottomN 累加器来查找每场比赛中得分最低的玩家。

db.gamescores.aggregate( [
{
$group:
{ _id: "$gameId", playerId:
{
$bottomN:
{
output: [ "$playerId","$score" ],
sortBy: { "score": -1 },
n: 3
}
}
}
}
] )

示例管道

  • 使用 $group 将结果按 gameId 分组。

  • 指定从 $bottomN 输出的字段,使用 output : ["$playerId", "$score"]

  • 使用 { "score": -1 } 对结果进行降序排序。

  • 使用 $bottomN 返回每个游戏得分最低的前三个文档,其中 n: 3

操作返回以下结果

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

此查询的 SQL 等价形式是

SELECT PLAYERID,GAMEID,SCORE
FROM(
SELECT ROW_NUMBER() OVER (PARTITION BY GAMEID ORDER BY SCORE DESC) AS GAMERANK,
GAMEID,PLAYERID,SCORE
FROM GAMESCORES
) AS T
WHERE GAMERANK >= 2
ORDER BY GAMEID

您也可以动态地分配 n 的值。在这个例子中,使用了 $cond 表达式在 gameId 字段上。

db.gamescores.aggregate([
{
$group:
{
_id: {"gameId": "$gameId"},
gamescores:
{
$bottomN:
{
output: "$score",
n: { $cond: { if: {$eq: ["$gameId","G2"] }, then: 1, else: 3 } },
sortBy: { "score": -1 }
}
}
}
}
] )

示例管道

  • 使用 $group 将结果按 gameId 分组。

  • 指定从 $bottomN 输出的字段,使用 output : "$score"

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

  • 使用 { "score": -1 } 对结果进行降序排序。

操作返回以下结果

[
{ _id: { gameId: "G2" }, gamescores: [ 10 ] },
{ _id: { gameId: "G1" }, gamescores: [ 33, 31, 1 ] }
]

返回

$bottom