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

$firstN

本页内容

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

新功能版本中5.2.

$firstN可以用作聚合累加器或数组操作符。作为聚合累加器,它返回组内前n个元素的聚合。作为数组操作符,它返回数组开头的指定数量的元素。

$firstN

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

当用作聚合累加器时,$firstN具有以下语法

{
$firstN:
{
input: <expression>,
n: <expression>
}
}
  • input指定从文档中获取前n个元素的字段。输入可以是任何表达式。

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

  • $firstN 不过滤空值。

  • $firstN 将缺失值转换为空值。

考虑以下聚合操作,该操作从一组中返回前五个文档

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",
firstFiveScores:
{
$firstN:
{
input: "$score",
n: 5
}
}
}
}
] )

在这个例子中

  • $documents 创建包含玩家得分的实际文档。

  • $group 按照游戏标识符 gameId 对文档进行分组。本例只有一个 gameId,即 G1

  • PlayerD 缺少得分,而 PlayerE 的得分是空值。这两个值都视为空值。

  • 使用 input : "$score" 指定 firstFiveScores 字段,并返回为数组。

  • 由于没有排序条件,返回前5个 score 字段。

[
{
_id: 'G1',
firstFiveScores: [ 1, 2, 3, null, null ]
}
]

$firstN$topN 累加器可以实现类似的结果。

通常

  • 如果进入 $group 的文档已经排序,则应使用 $firstN

  • 如果您正在排序并选择前 n 个元素,则可以使用 $topN 累加器通过一个累加器完成这两个任务。

  • $firstN可以用作聚合表达式,而$topN则不行。

$firstN支持作为聚合表达式。

$firstN支持作为窗口操作符

考虑一个包含以下文档的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 }
])

您可以使用累加器 $firstN 来查找单场比赛的前三个得分。

db.gamescores.aggregate( [
{
$match : { gameId : "G1" }
},
{
$group:
{
_id: "$gameId",
firstThreeScores:
{
$firstN:
{
input: ["$playerId", "$score"],
n:3
}
}
}
}
] )

示例管道

  • 使用 $match 在单个 gameId 上筛选结果。在这种情况下,G1

  • 使用 $group 按照游戏ID G1 对结果进行分组。

  • 使用 input : ["$playerId", "$score"] 指定提供给 $firstN 的字段。

  • 使用 $firstN 返回具有 n : 3G1 游戏的前三个文档。

该操作返回以下结果

[
{
_id: 'G1',
firstThreeScores: [ [ 'PlayerA', 31 ], [ 'PlayerB', 33 ], [ 'PlayerC', 99 ] ]
}
]

您可以使用累加器 $firstN 来查找每场比赛的前 n 输入字段。

db.gamescores.aggregate( [
{
$group:
{
_id: "$gameId", playerId:
{
$firstN:
{
input: [ "$playerId","$score" ],
n: 3
}
}
}
}
] )

示例管道

  • 使用 $group 按照游戏ID对结果进行分组。

  • 使用 $firstN 返回每场比赛的前三个文档,n: 3

  • 使用 input : ["$playerId", "$score"] 指定提供给 $firstN 的字段。

该操作返回以下结果

[
{
_id: 'G1',
playerId: [ [ 'PlayerA', 31 ], [ 'PlayerB', 33 ], [ 'PlayerC', 99 ] ]
},
{
_id: 'G2',
playerId: [ [ 'PlayerA', 10 ], [ 'PlayerB', 14 ], [ 'PlayerC', 66 ] ]
}
]

在管道中较早阶段使用 $sort 阶段可以影响 $firstN 累加器的结果。

在这个例子中

  • {$sort : { score : -1 } } 将每个组的最高分数排序到后面。

  • firstN 返回每个组前面的三个最高分数。

db.gamescores.aggregate( [
{ $sort : { score : -1 } },
{
$group:
{ _id: "$gameId", playerId:
{
$firstN:
{
input: [ "$playerId","$score" ],
n: 3
}
}
}
}
] )

该操作返回以下结果

[
{
_id: 'G2',
playerId: [ [ 'PlayerD', 80 ], [ 'PlayerC', 66 ], [ 'PlayerB', 14 ] ]
},
{
_id: 'G1',
playerId: [ [ 'PlayerC', 99 ], [ 'PlayerB', 33 ], [ 'PlayerA', 31 ] ]
}
]

您还可以动态地分配 n 的值。在这个例子中,$cond 表达式用于 gameId 字段。

db.gamescores.aggregate([
{
$group:
{
_id: {"gameId": "$gameId"},
gamescores:
{
$firstN:
{
input: "$score",
n: { $cond: { if: {$eq: ["$gameId","G2"] }, then: 1, else: 3 } }
}
}
}
}
] )

示例管道

  • 使用 $group 按照游戏ID对结果进行分组。

  • 使用 input : "$score" 指定 $firstN 的输入字段。

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

该操作返回以下结果

[
{ _id: { gameId: 'G1' }, gamescores: [ 31, 33, 99 ] },
{ _id: { gameId: 'G2' }, gamescores: [ 10 ] }
]

您还可以将 $firstN 作为聚合表达式使用。

在这个例子中

  • $documents 创建包含值数组的字面量文档。

  • $project 用于返回 $firstN 的输出。

  • _id 在输出中省略了,使用 _id : 0

  • $firstN 使用输入数组 [10, 20, 30, 40]

  • 返回输入文档的数组的前三个元素。

db.aggregate( [
{
$documents: [
{ array: [10, 20, 30, 40] } ]
},
{ $project: {
firstThreeElements:{
$firstN:
{
input: "$array",
n: 3
}
}
}
}
] )

该操作返回以下结果

[
{ firstThreeElements: [ 10, 20, 30 ] }
]
$firstN

当用作数组操作符时,$firstN 的语法如下

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

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

  • 不能指定小于 1n 值。

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

  • 如果 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 ]}
])

以下示例使用 $firstN 操作符检索每个玩家的前三个得分。得分以由 $addFields 创建的新字段 firstScores 返回。

db.games.aggregate([
{ $addFields: { firstScores: { $firstN: { n: 3, input: "$score" } } } }
])

该操作返回以下结果

[{
"playerId": 1,
"score": [ 1, 2, 3 ],
"firstScores": [ 1, 2, 3 ]
},
{
"playerId": 2,
"score": [ 12, 90, 7, 89, 8 ],
"firstScores": [ 12, 90, 7 ]
},
{
"playerId": 3,
"score": [ null ],
"firstScores": [ null ]
},
{
"playerId": 4,
"score": [ ],
"firstScores": [ ]
},
{
"playerId": 5,
"score": [ 1293, null, 3489, 9 ],
"firstScores": [ 1293, null, 3489 ]
},
{
"playerId": 6,
"score": [ "12.1", 2, NumberLong("2090845886852"), 23 ],
"firstScores": [ "12.1", 2, NumberLong("2090845886852") ]
}]

提示

另请参阅

返回

$first