Docs 菜单
Docs 主页
/
MongoDB Manual
/ / /

$topN(聚合累加器)

在此页面上

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

5.2 版本中的新增功能

根据指定的排序顺序,返回群组内前 n 个元素的聚合。如果该组包含的元素少于 n 个,$topN 返回该组中的所有元素。

{
$topN:
{
n: <expression>,
sortBy: { <field1>: <sort order>, <field2>: <sort order> ... },
output: <expression>
}
}
  • n 限制每组的结果数量,并且必须是正整数表达式,可以是常量或取决于 $group_id值。

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

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

  • $topN 不筛选空值。

  • $topN 将缺失值转换为 null 并保留在输出中。

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

在本例中:

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

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

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

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

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

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

对不同类型进行排序时,将使用BSON 数据类型的顺序来确定顺序。 例如,考虑一个其值由字符串和数字组成的collection。

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

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

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

在本例中:

  • PlayerA 有一个整数分数。

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

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

由于按降序{ "score" : -1 }排序,因此字符串字面值会排序在PlayerA的数值之前:

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

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

$topN 支持作为 window operator

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

可以使用 $topN 累加器查找单个游戏中得分最高的玩家。

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

示例管道:

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

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

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

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

  • 使用 $topN 返回 G1 游戏中带有 n : 3 的最高 score 的前三个文档。

操作返回以下结果:

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

与该查询等效的 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 DESC) AS T3
ON GS.GAMEID = T3.GAMEID
GROUP BY T3.GAMEID,T3.PLAYERID,T3.SCORE
ORDER BY T3.SCORE DESC

可以使用 $topN 累加器查找每个游戏中得分最高的玩家。

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

示例管道:

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

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

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

  • 使用 $topN 返回每个具有 n: 3 游戏的 score 最高的前三个文档。

操作返回以下结果:

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

与该查询等效的 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 <= 3
ORDER BY GAMEID

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

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

示例管道:

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

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

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

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

操作返回以下结果:

[
{ _id: { gameId: 'G1' }, gamescores: [ 99, 33, 31 ] },
{ _id: { gameId: 'G2' }, gamescores: [ 80 ] }
]

后退

$top

来年

$toString