定義
バージョン 5.2 で追加。
$firstN は 集計アキュムレータまたは 配列演算子として使用できます。 集計アキュムレータとして、グループ内の最初のn要素の集計を返します。 配列演算子として、配列の先頭から指定された数の要素を返します。
集計アキュムレータ
$firstNが集計アキュムレータとして使用されている場合、返される要素は、指定されたソート順序にある場合にのみ意味があります。 グループに含まれる要素がn未満の場合、 $firstNはグループ内のすべての要素を返します。
構文
集計アキュムレータとして使用する場合、 $firstNは次の構文をとります。
{ $firstN: { input: <expression>, n: <expression> } }
inputは、ドキュメントの最初のnを取得するフィールドを指定します。 入力は任意の式にできます。nは、定数であるか、$groupの_id値に依存する正の整数式である必要があります。 詳しくは、 グループキーの例を参照してください。
動作
NULL および欠損値
$firstNは、null 値をフィルタリングで除外しません。$firstN欠落値を null に変換します。
グループから最初の 5 つのドキュメントを返す次の集計を考えてみましょう。
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は 1 つのみです。PlayerDはスコアが欠落しており、PlayerEには nullscoreがあります。 これらの値は両方とも null と見なされます。firstFiveScoresフィールドはinput : "$score"を使用して指定され、配列として返されます。ソート条件がないため、最初の 5 つの
scoreフィールドが返されます。
[ { _id: 'G1', firstFiveScores: [ 1, 2, 3, null, null ] } ]
アキュムレータと アキュムレータの比較$firstN$topN
$firstNと$topNのどちらのアキュムレータでも同様の結果が得られます。
一般に、
$groupに取り込まれるドキュメントがすでに順序付けられている場合は、$firstNを使用する必要があります。上位の
n要素をソートして選択している場合、$topNを使用すると 1 つのアキュムレータで両方のタスクを実行できます。$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 } ])
1 つの一致の最初の 3 人のプレイヤー スコアを検索する
$firstNアキュムレータを使用して、1 つのゲームの最初の 3 つのスコアを検索できます。
db.gamescores.aggregate( [ { $match : { gameId : "G1" } }, { $group: { _id: "$gameId", firstThreeScores: { $firstN: { input: ["$playerId", "$score"], n:3 } } } } ] )
サンプル パイプライン:
$matchを使用して結果を単一のgameIdでフィルタリングします。 この場合は、G1ます。$groupを使用して結果をgameIdでグループ化します。 この場合は、G1ます。を使用して
$firstNinput : ["$playerId"," $score"]に入力するフィールドを指定します。$firstNを使用して、G1とn : 3の最初の 3 つのドキュメントを返します。
この操作は次の結果を返します。
[ { _id: 'G1', firstThreeScores: [ [ 'PlayerA', 31 ], [ 'PlayerB', 33 ], [ 'PlayerC', 99 ] ] } ]
複数のゲームにわたる最初の 3 人のユーザー スコアを検索する
$firstNアキュムレータを使用して、各ゲームの最初のn入力フィールドを見つけることができます。
db.gamescores.aggregate( [ { $group: { _id: "$gameId", playerId: { $firstN: { input: [ "$playerId","$score" ], n: 3 } } } } ] )
サンプル パイプライン:
$groupを使用して結果をgameIdでグループ化します。$firstNを使用して、n: 3を持つ各ゲームの最初の 3 つのドキュメントを返します。を使用して
$firstNinput : ["$playerId", "$score"]に入力するフィールドを指定します。
この操作は次の結果を返します。
[ { _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は各グループの前から 3 つの最高のスコアを返します。
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の値を動的に割り当てることもできます。 この例では、 $cond式はgameIdフィールドで使用されています。
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]の入力配列を使用します。入力ドキュメントには、配列の最初の 3 つの要素が返されます。
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演算子を使用して、各プレイヤーの最初の 3 つのスコアを検索します。 スコアは、 $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") ] }]