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

$topN(聚合累加器)

本页内容

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

版本5.2.

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

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

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

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

  • $topN 不过滤空值。

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

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 创建包含玩家得分的字面文档。

  • $group 按照文档中的 gameId 进行分组。本例中只有一个 gameId,即 G1

  • PlayerD 缺少分数,而 PlayerE 的分数为空。这两个值都被视为空值。

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

  • 由于 sortBy: { "score": 1 },null 值被排序到返回的 playerId 数组的开头。

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

在排序不同类型时,使用 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: {
$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 可以作为窗口操作符使用。

$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

  • 使用 $group 来根据 gameId 对结果进行分组。在本例中,G1

  • 使用按 { "score": -1 } 排序来按降序排序结果。

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

  • 使用 $topN 返回 G1 游戏中得分最高的前三个文档,其中 n : 3

操作返回以下结果

[
{
_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
}
}
}
}
] )

示例管道

  • 使用 $group 来根据 gameId 对结果进行分组。

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

  • 使用按 { "score": -1 } 排序来按降序排序结果。

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

操作返回以下结果

[
{
_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 的值。在此示例中,使用了 $cond 表达式在 gameId 字段上。

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

示例管道

  • 使用 $group 来根据 gameId 对结果进行分组。

  • 指定 $topN 输出的字段,其中 output : "$score"

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

  • 使用按 { "score": -1 } 排序来按降序排序结果。

操作返回以下结果

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

返回

顶部