文档菜单
文档首页
/
MongoDB 手册
/ / /

最后N个

在本页

  • 定义
  • 聚合累加器
  • 语法
  • 行为
  • 限制
  • 示例
  • 数组运算符
  • 语法
  • 行为
  • 示例

版本5.2.

$lastN可以用作聚合累加器或数组运算符。作为聚合累加器,它对组内最后n个元素进行聚合。作为数组运算符,它从数组的末尾返回指定数量的元素。

$lastN

$lastN用作聚合累加器时,返回的元素只有在指定排序顺序中才有意义。如果组中元素少于n个,则$lastN返回组中的所有元素。

{
$lastN:
{
input: <expression>,
n: <expression>
}
}
  • input指定从文档中取最后n的字段。输入可以是任何表达式。

  • n必须是一个正整数表达式,它可以是常量或者依赖于_id值,用于$group。详细信息请参阅分组键示例.

  • $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 缺少分数,而 PlayerEscore 为空。这两个值都被视为空。

  • 使用 input : "$score" 指定 lastFiveScores 字段,并以数组形式返回。

  • 由于没有排序标准,因此返回最后 5 个 score 字段。

[
{
_id: "G1",
lastFiveScores: [ 1, 2, 3, null, null ]
}
]

这两个累加器 $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 : { 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 的值。在这个例子中,使用 $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"

  • 如果 gameIdG2,则 n 为 1,否则 n 为 3。

该操作返回以下结果

[
{ _id: { gameId: "G1" }, gamescores: [ 33, 99, 1 ] },
{ _id: { gameId: "G2" }, gamescores: [ 80 ] }
]

您还可以将 $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 语法如下

{ $lastN: { n: <expression>, input: <expression> } }
字段
描述
n
一个解析为正整数的表达式。整数指定 $lastN 返回的数组元素数量。
input
一个解析为从其中返回 n 个元素的数组的表达式
  • $lastN 返回的元素顺序与输入数组中出现的顺序相同。

  • $lastN 不会过滤输入数组中的 null 值。

  • 不能指定小于 1n 值。

  • 如果指定的 n 大于或等于输入数组的元素数量,$lastN 返回输入数组。

  • 如果 input 解析为非数组值,聚合操作会出错。

集合 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 ]
}]

提示

另请参阅

返回

最后一个