定义
5.2 版本中的新增功能。
$firstN 可用作聚合累加器或数组操作符。 作为聚合累加器,它返回群组内前n个元素的聚合。 作为数组操作符,它会从数组开头返回指定数量的元素。
聚合累加器
当$firstN用作聚合累加器时,返回的元素仅在按指定排序顺序时才有意义。 如果该组包含的元素少于n个, $firstN将返回该组中的所有元素。
语法
当用作聚合累加器时,$firstN 具有以下语法:
{ $firstN: { input: <expression>, n: <expression> } }
行为
Null 和缺失值
$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的score为空。这些值都被视为空值。firstFiveScores字段使用input : "$score"指定并作为数组返回。由于没有排序条件,因此返回前 5 个
score字段。
[ { _id: 'G1', firstFiveScores: [ 1, 2, 3, null, null ] } ]
$firstN和$topN 累加器的比较
$firstN和$topN累加器都可以实现类似的结果。
一般来说:
如果进入
$group的文档已经排序,则应使用$firstN。如果您要排序并选择顶部的
n元素,则可以使用$topN通过一个累加器完成这两项任务。$firstN可用作聚合表达式,而$topN则不能。
限制
窗口函数和聚合表达式支持
$firstN 支持作为聚合表达式。
$firstN 支持作为 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 } ])
查找单场比赛的前三名玩家得分
可以使用 $firstN 累加器查找单个游戏中的前三个得分。
db.gamescores.aggregate( [ { $match : { gameId : "G1" } }, { $group: { _id: "$gameId", firstThreeScores: { $firstN: { input: ["$playerId", "$score"], n:3 } } } } ] )
示例管道:
使用
$match筛选单个gameId的结果。在本例中为G1。使用
$group按gameId对结果分组。本例中为G1。指定使用
input : ["$playerId"," $score"]为$firstN输入的字段。使用
$firstN返回具有n : 3的G1游戏的前三个文档。
操作返回以下结果:
[ { _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按gameId对结果进行分组。使用
$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阶段可能会影响$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 的群组密钥计算$group
您还可以动态分配n的值。 在此示例中,对gameId字段使用了$cond表达式。
db.gamescores.aggregate([ { $group: { _id: {"gameId": "$gameId"}, gamescores: { $firstN: { input: "$score", n: { $cond: { if: {$eq: ["$gameId","G2"] }, then: 1, else: 3 } } } } } } ] )
示例管道:
使用
$group按gameId对结果进行分组。指定使用
input : "$score"输入$firstN的字段。如果
gameId为G2,则n为 1,否则n为 3。
操作返回以下结果:
[ { _id: { gameId: 'G1' }, gamescores: [ 31, 33, 99 ] }, { _id: { gameId: 'G2' }, gamescores: [ 10 ] } ]
使用$firstN 作为聚合表达式
您还可以使用$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: { 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 ]} ])
以下示例使用 $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, Long("2090845886852"), 23 ], "firstScores": [ "12.1", 2, Long("2090845886852") ] }]