Docs Menu
Docs Home
/
MongoDB マニュアル
/ / /

$firstN

項目一覧

  • 定義
  • 集計アキュムレータ
  • 構文
  • 動作
  • 制限事項
  • 配列演算子
  • 構文
  • 動作

バージョン 5.2 で追加

$firstN は 集計アキュムレータまたは 配列演算子として使用できます。 集計アキュムレータとして、グループ内の最初のn要素の集計を返します。 配列演算子として、配列の先頭から指定された数の要素を返します。

$firstN

$firstNが集計アキュムレータとして使用されている場合、返される要素は、指定されたソート順序にある場合にのみ意味があります。 グループに含まれる要素がn未満の場合、 $firstNはグループ内のすべての要素を返します。

集計アキュムレータとして使用する場合、 $firstNは次の構文をとります。

{
$firstN:
{
input: <expression>,
n: <expression>
}
}
  • input は、ドキュメントの最初のnを取得するフィールドを指定します。 入力は任意の式にできます。

  • n は、定数であるか、 $group_id値に依存する正の整数式である必要があります。 詳しくは、 グループキーの例を参照してください。

  • $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でグループ化します。 この例ではgameIdG1は 1 つのみです。

  • PlayerD はスコアが欠落しており、 PlayerEには null scoreがあります。 これらの値は両方とも null と見なされます。

  • firstFiveScoresフィールドはinput : "$score"を使用して指定され、配列として返されます。

  • ソート条件がないため、最初の 5 つのscoreフィールドが返されます。

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

$firstN$topNのどちらのアキュムレータでも同様の結果が得られます。

一般に、

  • $groupに取り込まれるドキュメントがすでに順序付けられている場合は、 $firstNを使用する必要があります。

  • 上位のn要素をソートして選択している場合、 $topNを使用すると 1 つのアキュムレータで両方のタスクを実行できます。

  • $firstN は集計式として使用できますが、 $topNは使用できません。

$firstN集計式 としてサポートされています。

$firstNwindow 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アキュムレータを使用して、1 つのゲームの最初の 3 つのスコアを検索できます。

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

サンプル パイプライン:

  • $matchを使用して結果を単一のgameIdでフィルタリングします。 この場合は、 G1ます。

  • $groupを使用して結果をgameIdでグループ化します。 この場合は、 G1ます。

  • を使用して$firstN input : ["$playerId"," $score"]に入力するフィールドを指定します。

  • $firstNを使用して、 G1n : 3の最初の 3 つのドキュメントを返します。

この操作は次の結果を返します。

[
{
_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を持つ各ゲームの最初の 3 つのドキュメントを返します。

  • を使用して$firstN input : ["$playerId", "$score"]に入力するフィールドを指定します。

この操作は次の結果を返します。

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

パイプラインの前半で$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の値を動的に割り当てることもできます。 この例では、 $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に入力するフィールドを指定します。

  • 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]の入力配列を使用します。

  • 入力ドキュメントには、配列の最初の 3 つの要素が返されます。

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値をフィルタリングしません。

  • 1 nの値は指定できません。

  • 指定されたninput配列内の要素数以上の場合、 $firstNinput配列を返します。

  • 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演算子を使用して、各プレイヤーの最初の 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, NumberLong("2090845886852"), 23 ],
"firstScores": [ "12.1", 2, NumberLong("2090845886852") ]
}]

Tip

以下も参照してください。

戻る

$first