$bottomN(聚合累加器)
定义
语法
{ $bottomN: { n: <expression>, sortBy: { <field1>: <sort order>, <field2>: <sort order> ... }, output: <expression> } }
行为
空值和缺失值
$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
创建包含玩家得分的文档。$group
按gameId
对文档进行分组。本例中只有一个gameId
,即G1
。PlayerD
有一个缺失的分数,而PlayerE
有一个空score
。这两个值都被视为空值。将
playerId
和score
字段指定为output : ["$playerId", "$score"]
,并以数组值返回。由于
sortBy: { "score" : -1 }
,空值被排序到返回的playerId
数组末尾。
[ { _id: "G1", playerId: [ [ "PlayerA", 1 ], [ "PlayerD", null ], [ "PlayerE", null ] ] } ]
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: { $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
根据 $group
的组键计算 n
您也可以动态地分配 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"
。如果
gameId
是G2
,则n
为 1,否则为 3。使用
{ "score": -1 }
对结果进行降序排序。
操作返回以下结果
[ { _id: { gameId: "G2" }, gamescores: [ 10 ] }, { _id: { gameId: "G1" }, gamescores: [ 33, 31, 1 ] } ]