定義
バージョン 5.2 で追加。
$lastN は 集計アキュムレータまたは 配列演算子として使用できます。 集計アキュムレータとしては、グループ内の最後のn要素の集計です。 配列演算子として、配列の末尾から指定された数の要素を返します。
集計アキュムレータ
$lasttNが集計アキュムレータとして使用されている場合、返される要素は、指定されたソート順序にある場合にのみ意味があります。 グループに含まれる要素がn未満の場合、 $lastNはグループ内のすべての要素を返します。
構文
{ $lastN: { input: <expression>, n: <expression> } }
inputは、ドキュメントから最後のnを取得するフィールドを指定します。 入力は任意の式にできます。nは、定数であるか、$groupの_id値に依存する正の整数式である必要があります。 詳しくは、 グループキーの例を参照してください。
動作
NULL および欠損値
$lastNは、null 値をフィルタリングで除外しません。$lastN欠落値を 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", lastFiveScores: { $lastN: { input: "$score", n: 5 } } } } ] )
この例では、次のことが行われます。
$documentsは、プレイヤーのスコアを含むリテラル ドキュメントを作成します。$groupはドキュメントをgameIdでグループ化します。 この例ではgameId、G1は 1 つのみです。PlayerDはスコアが欠落しており、PlayerEには nullscoreがあります。 これらの値は両方とも null と見なされます。lastFiveScoresフィールドはinput : "$score"を使用して指定され、配列として返されます。ソート条件がないため、最後の 5 つの
scoreフィールドが返されます。
[ { _id: "G1", lastFiveScores: [ 1, 2, 3, null, null ] } ]
と の比較$lastN$bottomN
$lastNと$bottomNのどちらのアキュムレータでも同様の結果が得られます。
一般に、
$groupに取り込まれるドキュメントがすでに順序付けられている場合は、$lastNを使用する必要があります。最下位の
n要素をソートして選択している場合、$bottomNを使用すると 1 つのアキュムレータで両方のタスクを実行できます。$lastNは集計式として使用できますが、$bottomNは使用できません。
制限事項
ウィンドウ関数と集計式のサポート
$lastN は集計式 としてサポートされています。
$lastN は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 人のプレイヤー スコアを検索する
$lastNアキュムレータを使用して、1 つの一致の最後の 3 つのスコアを検索できます。
db.gamescores.aggregate( [ { $match : { gameId : "G1" } }, { $group: { _id: "$gameId", lastThreeScores: { $lastN: { input: ["$playerId", "$score"], n:3 } } } } ] )
サンプル パイプライン:
$matchを使用して結果を単一のgameIdでフィルタリングします。 この場合は、G1ます。$groupを使用して結果をgameIdでグループ化します。 この場合は、G1ます。を使用して
$lastNoutput : ["$playerId"," $score"]から出力するフィールドを指定します。$lastNを使用して、n : 3とG1のゲームの最後の 3 つのドキュメントを返します。
この操作は次の結果を返します。
[ { _id: "G1", lastThreeScores: [ [ "PlayerB", 33 ], [ "PlayerC", 99 ], [ "PlayerD", 1 ] ] } ]
複数のゲームにわたる最後の 3 人のプレイヤー スコアの検索
$lastNアキュムレータを使用して、各ゲーム内の最後のn入力フィールドを見つけることができます。
db.gamescores.aggregate( [ { $group: { _id: "$gameId", playerId: { $lastN: { input: [ "$playerId","$score" ], n: 3 } } } } ] )
サンプル パイプライン:
$groupを使用して結果をgameIdでグループ化します。$lastNを使用して、n: 3を持つ各ゲームの最後の 3 つのドキュメントを返します。を使用して
$lastNinput : ["$playerId", "$score"]に入力するフィールドを指定します。
この操作は次の結果を返します。
[ { _id: 'G2', playerId: [ [ 'PlayerB', 14 ], [ 'PlayerC', 66 ], [ 'PlayerD', 80 ] ] }, { _id: 'G1', playerId: [ [ 'PlayerB', 33 ], [ 'PlayerC', 99 ], [ 'PlayerD', 1 ] ] } ]
と の使用$sort$lastN
パイプラインの前半で$sortステージを使用すると、 $lastNアキュムレータの結果に影響する可能性があります。
この例では、次のことが行われます。
{$sort : { score : -1 } }は最高スコアを各グループの後ろにソートします。lastNは、各グループの後ろから 3 つの最低スコアを返します。
db.gamescores.aggregate( [ { $sort : { score : -1 } }, { $group: { _id: "$gameId", playerId: { $lastN: { input: [ "$playerId","$score" ], n: 3 } } } } ] )
この操作は次の結果を返します。
[ { _id: 'G2', playerId: [ [ 'PlayerC', 66 ], [ 'PlayerB', 14 ], [ 'PlayerA', 10 ] ] }, { _id: 'G1', playerId: [ [ 'PlayerB', 33 ], [ 'PlayerA', 31 ], [ 'PlayerD', 1 ] ] } ]
のグループキーに基づいて を計算n$group
また、 nの値を動的に割り当てることもできます。 この例では、 $cond式はgameIdフィールドで使用されています。
db.gamescores.aggregate([ { $group: { _id: {"gameId": "$gameId"}, gamescores: { $lastN: { input: "$score", n: { $cond: { if: {$eq: ["$gameId","G2"] }, then: 1, else: 3 } } } } } } ] )
サンプル パイプライン:
$groupを使用して結果をgameIdでグループ化します。input : "$score"を使用して$lastNに入力するフィールドを指定します。gameIdがG2の場合、nは 1、それ以外の場合、nは 3 になります。
この操作は次の結果を返します。
[ { _id: { gameId: "G1" }, gamescores: [ 33, 99, 1 ] }, { _id: { gameId: "G2" }, gamescores: [ 80 ] } ]
集計式として$lastN を使用
また、集計式として$lastNを使用することもできます。
この例では、次のことが行われます。
$documentsは、値の配列を含むリテラル ドキュメントを作成します。$projectは$lastNの出力を返すために使用されます。_idは_id : 0により出力から省略されます。$lastNは[10, 20, 30, 40]の入力配列を使用します。入力ドキュメントには、 配列の最後の 3 つの要素が返されます。
db.aggregate( [ { $documents: [ { array: [10, 20, 30, 40] } ] }, { $project: { lastThreeElements:{ $lastN: { input: "$array", n: 3 } } } } ] )
この操作は次の結果を返します。
[ { lastThreeElements: [ 20, 30, 40 ] } ]
配列演算子
構文
$lastN の構文は次のとおりです。
{ $lastN: { 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 ]} ])
次の例では、 $lastN演算子を使用して、各プレイヤーの最後の 3 つのスコアを検索します。 スコアは、 $addFieldsによって作成された新しいフィールドlastScoresに返されます。
db.games.aggregate([ { $addFields: { lastScores: { $lastN: { n: 3, input: "$score" } } } } ])
この操作は次の結果を返します。
[{ "playerId": 1, "score": [ 1, 2, 3 ], "lastScores": [ 1, 2, 3 ] }, { "playerId": 2, "score": [ 12, 90, 7, 89, 8 ], "lastScores": [ 7, 89, 8 ] }, { "playerId": 3, "score": [ null ], "lastScores": [ null ] }, { "playerId": 4, "score": [ ], "lastScores": [ ] }, { "playerId": 5, "score": [ 1293, null, 3489, 9 ], "lastScores": [ null, 3489, 9 ] }, { "playerId": 6, "score": [ "12.1", 2, Long("2090845886852"), 23 ], "lastScores": [ 2, Long("2090845886852"), 23 ] }]