$firstN
定义
新功能版本中5.2.
$firstN
可以用作聚合累加器或数组操作符。作为聚合累加器,它返回组内前n
个元素的聚合。作为数组操作符,它返回数组开头的指定数量的元素。
聚合累加器
当$firstN
用作聚合累加器时,返回的元素只有在指定排序顺序下才有意义。如果分组包含的元素少于n
个,则$firstN
返回组中的所有元素。
语法
当用作聚合累加器时,$firstN
具有以下语法
{ $firstN: { input: <expression>, n: <expression> } }
行为
空值和缺失值
$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
累加器的比较
$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
按照游戏IDG1
对结果进行分组。使用
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
按照游戏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
阶段可以影响 $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 ] ] } ]
基于分组键计算 $group
的 n
您还可以动态地分配 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
的输入字段。如果
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, 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") ] }]