$bottomN
聚合运算符返回分组中指定顺序的最后n
个元素,如果分组中的元素数量小于n
,则返回分组的全部元素。从MongoDB5.2开始支持。
{
$bottomN:
{
n: <expression>,
sortBy: { <field1>: <sort order>, <field2>: <sort order> ... },
output: <expression>
}
}
n
用于限制每组结果的数量,必须是正整数表达式,要么是常数,要么取决于$group
的_id
值sortBy
制定返回结果的顺序,语法类似于$sort
output
指定分组元素输出的内容,可以是任何合法的表达式。$bottom
不支持作为聚合表达式。$bottom
只支持作为window 操作符
。$bottom
受100M的限制,如果单组超过这一限制将报错。$bottom
不会过滤掉空值$bottom
会将缺失值转换为nulldb.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
的得分为null
,他们的得分都会被当做null
处理playerId
字段和score
字段被指定为输出:["$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
的得分是整数1
PlayerB
的得分是字符串"2"
PlayerC
的得分是空字符串""
因为排序指定为逆序{ "score" : -1 }
,字符串的字面量排在PlayerA
的数值得分之前:
[
{
_id: "G1",
playerId: [ [ "PlayerB", "2" ], [ "PlayerC", "" ], [ "PlayerA", 1 ] ]
}
]
使用下面的命令创建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
查找单个游戏中得分最低的3个:
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
output : ["$playerId"," $score"]
为bottom
指定输出字段sortBy: { "score": -1 }
按照得分进行逆序排序$bottomN
返回游戏得分最低的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
按照groupId
对结果排序output : ["$playerId", "$score"]
指定bottom
输出的字段sortBy: { "score": -1 }
按照得分进行逆序排序$bottomN
返回所有游戏中得分最低的三个结果如下:
[
{
_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
按照groupId
对结果排序output : "$score"
指定$bottomN
输出的字段gameId
是G2
则n
为1,否则n
为3sortBy: { "score": -1 }
按照得分进行逆序排序操作结果如下:
[
{ _id: { gameId: "G2" }, gamescores: [ 10 ] },
{ _id: { gameId: "G1" }, gamescores: [ 33, 31, 1 ] }
]