$topN(聚合累加器)
定义
语法
{ $topN: { n: <expression>, sortBy: { <field1>: <sort order>, <field2>: <sort order> ... }, output: <expression> } }
行为
空值和缺失值
$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
的分数为空。这两个值都被视为空值。字段
playerId
和score
被指定为output : ["$playerId", "$score"]
并作为数组值返回。由于
sortBy: { "score": 1 }
,null 值被排序到返回的playerId
数组的开头。
[ { _id: 'G1', playerId: [ [ 'PlayerD', null ], [ 'PlayerE', null ], [ 'PlayerA', 1 ] ] } ]
BSON 数据类型排序顺序
在排序不同类型时,使用 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
,适用于 $group
您还可以动态分配 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"
。如果
gameId
是G2
,则n
为 1,否则n
为 3。使用按
{ "score": -1 }
排序来按降序排序结果。
操作返回以下结果
[ { _id: { gameId: 'G1' }, gamescores: [ 99, 33, 31 ] }, { _id: { gameId: 'G2' }, gamescores: [ 80 ] } ]