On this page本页内容
$lastN
New in version 5.2.在版本5.2中新增。
Returns an aggregation of the last 返回组中最后n
elements within a group. n
个元素的聚合。The elements returned are meaningful only if in a specified sort order. 只有按指定的排序顺序返回的元素才有意义。If the group contains fewer than 如果组包含的元素少于n
elements, $lastN
returns all elements in the group.n
个,$lastN
将返回组中的所有元素。
{ $lastN: { input: <expression>, n: <expression> } }
input
n
of. n
个的字段。n
_id
value for $group
. $group
的_id
值。Null
和缺少值$lastN
$lastN
null
。Consider the following aggregation that returns the last five documents from a group:考虑以下从组中返回最后五个文档的聚合:
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 } } } } ] )
In this example:在此示例中:
$documents
$group
gameId
. gameId
对文档进行分组。gameId
, G1
.gameId
,G1
。PlayerD
PlayerE
has a null score
. score
,而PlayerE
的score
为空。null
。lastFiveScores
field is specified using input : "$score"
and returned as an array.lastFiveScores
字段是使用input : "$score"
指定的,并作为数组返回。score
fields are returned.score
字段。[ { _id: "G1", lastFiveScores: [ 1, 2, 3, null, null ] } ]
$lastN
and $bottomN
$lastN
和$bottomN
的比较Both $lastN
and $bottomN
accumulators can accomplish similar results.$lastN
和$bottomN
累加器都可以实现类似的结果。
In general:一般来说:
$group
are already ordered, you should use $lastN
.$group
的文档已经订购,则应使用$lastN
。n
elements then you can use $bottomN
to accomplish both tasks with one accumulator.n
个元素,那么可以使用$bottomN
用一个累加器完成这两个任务。$lastN
can be used as an aggregation expression, $bottomN
cannot.$lastN
可以用作聚合表达式,$bottomN
不能。$lastN
is supported as an aggregation expression.$lastN
支持作为聚合表达式。
For details on aggregation expression usage see Using $lastN as an Aggregation Expression.有关聚合表达式用法的详细信息,请参阅使用$lastN
作为聚合表达式。
$lastN
is supported as a window operator.支持作为窗口运算符。
Aggregation pipelines which call 调用$lastN
are subject to the 100 MB limit. $lastN
的聚合管道受100 MB限制。If this limit is exceeded for an individual group, the aggregation fails with an error.如果单个组超过了此限制,则聚合将失败并返回错误。
Consider a 考虑一个包含以下文档的gamescores
collection with the following documents: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 } ])
You can use the 您可以使用$lastN
accumulator to find the last three scores in a single game.$lastN
累加器查找单个游戏中的最后三个分数。
db.gamescores.aggregate( [ { $match : { gameId : "G1" } }, { $group: { _id: "$gameId", lastThreeScores: { $lastN: { input: ["$playerId", "$score"], n:3 } } } } ] )
The example pipeline:示例管道:
$match
to filter the results on a single gameId
. $match
筛选单个gameId
上的结果。G1
.G1
。$group
to group the results by gameId
. $group
按gameId
对结果进行分组。G1
.G1
。$lastN
with output : ["$playerId"," $score"]
.output : ["$playerId"," $score"]
指定从$lastN
输出的字段。$lastN
to return the last three documents for the G1
game with n : 3
.$lastN
返回编号为n:3
的G1
游戏的最后三个文档。The operation returns the following results:该操作返回以下结果:
[ { _id: "G1", lastThreeScores: [ [ "PlayerB", 33 ], [ "PlayerC", 99 ], [ "PlayerD", 1 ] ] } ]
You can use the 您可以使用$lastN
accumulator to find the last n
input fields in each game.$lastN
累加器查找每个游戏中最后n
个输入字段。
db.gamescores.aggregate( [ { $group: { _id: "$gameId", playerId: { $lastN: { input: [ "$playerId","$score" ], n: 3 } } } } ] )
The example pipeline:示例管道:
$group
to group the results by gameId
.$group
按gameId
对结果进行分组。$lastN
to return the last three documents for each game with n: 3
.$lastN
返回n:3
的每个游戏的最后三个文档。$lastN
with input : ["$playerId", "$score"]
.input : ["$playerId", "$score"]
指定$lastN
的输入字段。The operation returns the following results:该操作返回以下结果:
[ { _id: 'G2', playerId: [ [ 'PlayerB', 14 ], [ 'PlayerC', 66 ], [ 'PlayerD', 80 ] ] }, { _id: 'G1', playerId: [ [ 'PlayerB', 33 ], [ 'PlayerC', 99 ], [ 'PlayerD', 1 ] ] } ]
$sort
With $lastN
$sort
与$lastN
一起使用Using a 在管道的前面使用$sort
stage earlier in the pipeline can influence the results of the $lastN
accumulator.$sort
阶段可能会影响$lastN
累加器的结果。
In this example:在此示例中:
{$sort : { score : -1 } }
lastN
db.gamescores.aggregate( [ { $sort : { score : -1 } }, { $group: { _id: "$gameId", playerId: { $lastN: { input: [ "$playerId","$score" ], n: 3 } } } } ] )
The operation returns the following results:该操作返回以下结果:
[ { _id: 'G2', playerId: [ [ 'PlayerC', 66 ], [ 'PlayerB', 14 ], [ 'PlayerA', 10 ] ] }, { _id: 'G1', playerId: [ [ 'PlayerB', 33 ], [ 'PlayerA', 31 ], [ 'PlayerD', 1 ] ] } ]
n
Based on the Group Key for $group
$Group
的组密钥计算n
You can also assign the value of 还可以动态指定n
dynamically. n
的值。In this example, the 在本例中,$cond
expression is used on the gameId
field.$cond
表达式用于gameId
字段。
db.gamescores.aggregate([ { $group: { _id: {"gameId": "$gameId"}, gamescores: { $lastN: { input: "$score", n: { $cond: { if: {$eq: ["$gameId","G2"] }, then: 1, else: 3 } } } } } } ] )
The example pipeline:示例管道:
$group
to group the results by gameId
.$group
按gameId
对结果进行分组。$lastN
with input : "$score"
.input : "$score"
指定$lastN
的输入的字段。gameId
is G2
then n
is 1, otherwise n
is 3.gameId
为G2
,则n
为1
,否则n
为3
。The operation returns the following results:该操作返回以下结果:
[ { _id: { gameId: "G2" }, gamescores: [ 10 ] }, { _id: { gameId: "G1" }, gamescores: [ 33, 31, 1 ] } ]
$lastN
as an Aggregation Expression$lastN
作为聚合表达式You can also use 还可以使用$lastN
as an aggregation expression.$lastN
作为聚合表达式。
In this example:在此示例中:
$documents
$project
$lastN
.$lastN
的输出。_id
_id : 0
._id:0
的输出中省略。$lastN
[10, 20, 30, 40]
.[10, 20, 30, 40]
。db.aggregate( [ { $documents: [ { array: [10, 20, 30, 40] } ] }, { $project: { lastThreeElements:{ $lastN: { input: "$array", n: 3 } } } } ] )
The operation returns the following results:该操作返回以下结果:
[ { lastThreeElements: [ 20, 30, 40 ] } ]