最后N个
定义
新版本5.2.
$lastN
可以用作聚合累加器或数组运算符。作为聚合累加器,它对组内最后n
个元素进行聚合。作为数组运算符,它从数组的末尾返回指定数量的元素。
聚合累加器
当$lastN
用作聚合累加器时,返回的元素只有在指定排序顺序中才有意义。如果组中元素少于n
个,则$lastN
返回组中的所有元素。
语法
{ $lastN: { input: <expression>, n: <expression> } }
行为
空值和缺失值
$lastN
不会过滤空值。$lastN
将缺失值转换为空值。
考虑以下聚合,它返回组中的最后五个文档
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", lastFiveScores: { $lastN: { input: "$score", n: 5 } } } } ] )
在这个例子中
$documents
创建包含玩家得分的文档。$group
根据gameId
对文档进行分组。这个例子只有一个gameId
,即G1
。PlayerD
缺少分数,而PlayerE
的score
为空。这两个值都被视为空。使用
input : "$score"
指定lastFiveScores
字段,并以数组形式返回。由于没有排序标准,因此返回最后 5 个
score
字段。
[ { _id: "G1", lastFiveScores: [ 1, 2, 3, null, null ] } ]
$lastN
和 $bottomN
的比较
这两个累加器 $lastN
和 $bottomN
都可以完成类似的结果。
一般来说
如果进入
$group
的文档已经排序,则应使用$lastN
。如果您正在排序并选择底部
n
个元素,则可以使用$bottomN
来使用一个累加器完成这两个任务。$lastN
可以用作聚合表达式,而$bottomN
不能。
限制
窗口函数和聚合表达式支持
$lastN
支持作为聚合表达式。
$lastN
支持作为窗口操作符。
示例
考虑以下文档的 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 } ])
查找单个游戏中的最后三个玩家得分
可以使用 $lastN
累加器来查找单个游戏中的最后三个得分。
db.gamescores.aggregate( [ { $match : { gameId : "G1" } }, { $group: { _id: "$gameId", lastThreeScores: { $lastN: { input: ["$playerId", "$score"], n:3 } } } } ] )
示例管道
使用
$match
过滤结果,仅针对单个gameId
。在这种情况下,G1
。使用
$group
将结果按gameId
分组。在这种情况下,G1
。指定由
$lastN
输出的字段,使用output : ["$playerId"," $score"]
。使用
$lastN
返回G1
游戏的最后三个文档,其中n : 3
。
该操作返回以下结果
[ { _id: "G1", lastThreeScores: [ [ "PlayerB", 33 ], [ "PlayerC", 99 ], [ "PlayerD", 1 ] ] } ]
在多场比赛中查找最后三位玩家得分
您可以使用累加器 $lastN
来查找每场比赛中的最后 n
个输入字段。
db.gamescores.aggregate( [ { $group: { _id: "$gameId", playerId: { $lastN: { input: [ "$playerId","$score" ], n: 3 } } } } ] )
示例管道
使用
$group
根据gameId
对结果进行分组。使用
$lastN
返回每个游戏中的最后三个文档,其中n: 3
。指定
$lastN
的输入字段,其中input : ["$playerId", "$score"]
。
该操作返回以下结果
[ { _id: 'G2', playerId: [ [ 'PlayerB', 14 ], [ 'PlayerC', 66 ], [ 'PlayerD', 80 ] ] }, { _id: 'G1', playerId: [ [ 'PlayerB', 33 ], [ 'PlayerC', 99 ], [ 'PlayerD', 1 ] ] } ]
结合使用 $sort
和 $lastN
在管道中较早的阶段使用 $sort
阶段可以影响累加器 $lastN
的结果。
在这个例子中
{$sort : { score : -1 } }
将每个组中的最高分放到后面。lastN
从每个组的后面返回三个最低分。
db.gamescores.aggregate( [ { $sort : { score : -1 } }, { $group: { _id: "$gameId", playerId: { $lastN: { input: [ "$playerId","$score" ], n: 3 } } } } ] )
该操作返回以下结果
[ { _id: 'G2', playerId: [ [ 'PlayerC', 66 ], [ 'PlayerB', 14 ], [ 'PlayerA', 10 ] ] }, { _id: 'G1', playerId: [ [ 'PlayerB', 33 ], [ 'PlayerA', 31 ], [ 'PlayerD', 1 ] ] } ]
根据 $group
的分组键计算 n
您还可以动态地分配 n
的值。在这个例子中,使用 $cond
表达式在 gameId
字段上。
db.gamescores.aggregate([ { $group: { _id: {"gameId": "$gameId"}, gamescores: { $lastN: { input: "$score", n: { $cond: { if: {$eq: ["$gameId","G2"] }, then: 1, else: 3 } } } } } } ] )
示例管道
使用
$group
根据gameId
对结果进行分组。指定了向
$lastN
输入时的字段,使用input : "$score"
。如果
gameId
是G2
,则n
为 1,否则n
为 3。
该操作返回以下结果
[ { _id: { gameId: "G1" }, gamescores: [ 33, 99, 1 ] }, { _id: { gameId: "G2" }, gamescores: [ 80 ] } ]
将 $lastN
作为聚合表达式使用
您还可以将 $lastN
作为聚合表达式使用。
在这个例子中
$documents
创建包含值数组的字面文档。使用
$project
返回$lastN
的输出。使用
_id : 0
省略了输出中的_id
。$lastN
使用输入数组[10, 20, 30, 40]
。对于输入文档返回数组的最后三个元素。
db.aggregate( [ { $documents: [ { array: [10, 20, 30, 40] } ] }, { $project: { lastThreeElements:{ $lastN: { input: "$array", n: 3 } } } } ] )
该操作返回以下结果
[ { lastThreeElements: [ 20, 30, 40 ] } ]
数组操作符
语法
$lastN
语法如下
{ $lastN: { n: <expression>, input: <expression> } }
行为
示例
集合 games
包含以下文档
db.games.insertMany([ { "playerId" : 1, "score" : [ 1, 2, 3 ] }, { "playerId" : 2, "score" : [ 12, 90, 7, 89, 8 ] }, { "playerId" : 3, "score" : [ null ] }, { "playerId" : 4, "score" : [ ] }, { "playerId" : 5, "score" : [ 1293, null, 3489, 9 ]}, { "playerId" : 6, "score" : [ "12.1", 2, NumberLong("2090845886852"), 23 ]} ])
以下示例使用 $lastN
操作符检索每个玩家的最后三个分数。分数将返回到由 $addFields
创建的新字段 lastScores
中。
db.games.aggregate([ { $addFields: { lastScores: { $lastN: { n: 3, input: "$score" } } } } ])
该操作返回以下结果
[{ "playerId": 1, "score": [ 1, 2, 3 ], "lastScores": [ 1, 2, 3 ] }, { "playerId": 2, "score": [ 12, 90, 7, 89, 8 ], "lastScores": [ 7, 89, 8 ] }, { "playerId": 3, "score": [ null ], "lastScores": [ null ] }, { "playerId": 4, "score": [ ], "lastScores": [ ] }, { "playerId": 5, "score": [ 1293, null, 3489, 9 ], "lastScores": [ null, 3489, 9 ] }, { "playerId": 6, "score": [ "12.1", 2, NumberLong("2090845886852"), 23 ], "lastScores": [ 2, NumberLong("2090845886852"), 23 ] }]