定义
5.2 版本中的新增功能。
$lastN 可用作聚合累加器或数组操作符。 作为聚合累加器,它是群组中最后n个元素的聚合。 作为数组操作符,它从数组末尾返回指定数量的元素。
聚合累加器
当$lasttN用作聚合累加器时,返回的元素仅在按指定排序顺序时才有意义。 如果该组包含的元素少于n个, $lastN将返回该组中的所有元素。
语法
{ $lastN: { input: <expression>, n: <expression> } }
行为
Null 和缺失值
$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为空。这些值都被视为空值。lastFiveScores字段使用input : "$score"指定并作为数组返回。由于没有排序条件,因此返回最后 5 个
score字段。
[ { _id: "G1", lastFiveScores: [ 1, 2, 3, null, null ] } ]
和 的比较$lastN$bottomN
$lastN和$bottomN累加器都可以实现类似的结果。
一般来说:
如果进入
$group的文档已经排序,则应使用$lastN。如果您要排序并选择底部的
n元素,则可以使用$bottomN通过一个累加器完成这两项任务。$lastN可用作聚合表达式,而$bottomN则不能。
限制
窗口函数和聚合表达式支持
$lastN 支持作为聚合表达式。
$lastN 支持作为 window operator。
示例
请考虑包含以下文档的 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。使用
output : ["$playerId"," $score"]指定从$lastN输出的字段。使用
$lastN返回具有n : 3的G1游戏的最后三个文档。
操作返回以下结果:
[ { _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的游戏的最后三个文档。指定使用
input : ["$playerId", "$score"]为$lastN输入的字段。
操作返回以下结果:
[ { _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 ] ] } ]
根据n 的群组密钥计算$group
您还可以动态分配n的值。 在此示例中,对gameId字段使用了$cond表达式。
db.gamescores.aggregate([ { $group: { _id: {"gameId": "$gameId"}, gamescores: { $lastN: { input: "$score", n: { $cond: { if: {$eq: ["$gameId","G2"] }, then: 1, else: 3 } } } } } } ] )
示例管道:
使用
$group按gameId对结果进行分组。指定使用
input : "$score"输入$lastN的字段。如果
gameId为G2,则n为 1,否则n为 3。
操作返回以下结果:
[ { _id: { gameId: "G1" }, gamescores: [ 33, 99, 1 ] }, { _id: { gameId: "G2" }, gamescores: [ 80 ] } ]
使用$lastN 作为聚合表达式
您还可以使用$lastN作为聚合表达式。
在本例中:
$documents创建包含值数组的字面文档。$project用于返回$lastN的输出。_id通过_id : 0从输出中省略。$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, Long("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, Long("2090845886852"), 23 ], "lastScores": [ 2, Long("2090845886852"), 23 ] }]