定義
構文
{ $minN: { input: <expression>, n: <expression> } }
inputは、$minNへの入力である 式 を指定します。 グループ内の各要素に対して評価が行われ、$minNは最小のn値を保持します。nはグループあたりの結果数を制限し、nは定数であるか、$groupの_id値に依存する正の整数式である必要があります。
動作
NULL および欠損値
$minNは null 値と欠損値を除外します。
グループから最小のnドキュメントを返す次の集計を検討してください。
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", minimumThreeScores: { $minN: { input: "$score", n: 4 } } } } ] )
この例では、次のことが行われます。
$documentsは、プレイヤーのスコアを含むリテラル ドキュメントを作成します。$groupはドキュメントをgameIdでグループ化します。 この例ではgameId、G1は 1 つのみです。PlayerDはスコアが欠落しており、PlayerEには nullscoreがあります。 これらの値は両方とも無視されます。minimumThreeScoresフィールドはinput : "$score"を持つ$minNとして指定され、配列として返されます。scoresを持つドキュメントは 3 つしかないため、minNはn = 4であっても少なくとも 3 つのscoreフィールドを返します。
[ { _id: 'G1', minimumThreeScores: [ 1, 2, 3 ] } ]
アキュムレータと アキュムレータの比較$minN$bottomN
$minNと$bottomNのどちらのアキュムレータでも同様の結果が得られます。
一般に、
$minNには、特定のソート順序で最小値が見つからないという利点があります。nドキュメントの最小値を確認するには、$minNを使用します。特定の並べ替え順序を保証する必要がある場合は、
$bottomNを使用します。出力値でソートしない場合は、
$bottomNを使用します。
制限事項
ウィンドウ関数と集計式のサポート
$minNをアキュムレータとして使用できます。
メモリ制限に関する考慮事項
$minNを呼び出す集計パイプラインには100 MB の制限が適用されます。 個々のグループでこの制限を超えると、集計はエラーで失敗します。
例
以下のドキュメントを持つ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 回のプレイに最小限必要な 3Scores つの を見つける
$minNアキュムレータを使用して、1 一致の最小スコア 3 を見つけることができます。
db.gamescores.aggregate( [ { $match : { gameId : "G1" } }, { $group: { _id: "$gameId", minScores: { $minN: { input: ["$score","$playerId"], n:3 } } } } ] )
サンプル パイプライン:
$matchを使用して結果を単一のgameIdでフィルタリングします。 この場合は、G1ます。$groupを使用して結果をgameIdでグループ化します。 この場合は、G1ます。を使用して
$minNinput : ["$score","$playerId"]に入力するフィールドを指定します。$minNを使用して、G1とn : 3の最初の 3 つのスコア要素を返します。
この操作は次の結果を返します。
[ { _id: 'G1', minScores: [ [ 1, 'PlayerD' ], [ 31, 'PlayerA' ], [ 33, 'PlayerB' ] ] } ]
複数のゲームにまたがって最小 3 つのドキュメントを検索
$minNアキュムレータを使用して、各ゲームの最小nスコアを見つけることができます。
db.gamescores.aggregate( [ { $group: { _id: "$gameId", minScores: { $minN: { input: ["$score","$playerId"], n: 3 } } } } ] )
サンプル パイプライン:
$groupを使用して結果をgameIdでグループ化します。$minNを使用して、n: 3を持つゲームごとの最小 3 つのスコア要素を返します。を使用して
$minNinput: ["$score","$playerId"]に入力するフィールドを指定します。
この操作は次の結果を返します。
[ { _id: 'G2', minScores: [ [ 10, 'PlayerA' ], [ 14, 'PlayerB' ], [ 66, 'PlayerC' ] ] }, { _id: 'G1', minScores: [ [ 1, 'PlayerD' ], [ 31, 'PlayerA' ], [ 33, 'PlayerB' ] ] } ]
のグループキーに基づいて を計算n$group
また、 nの値を動的に割り当てることもできます。 この例では、 $cond式はgameIdフィールドで使用されています。
db.gamescores.aggregate([ { $group: { _id: {"gameId": "$gameId"}, gamescores: { $minN: { input: ["$score","$playerId"], n: { $cond: { if: {$eq: ["$gameId","G2"] }, then: 1, else: 3 } } } } } } ] )
サンプル パイプライン:
$groupを使用して結果をgameIdでグループ化します。input : ["$score","$playerId"]を使用して$minNに入力するフィールドを指定します。gameIdがG2の場合、nは 1、それ以外の場合、nは 3 になります。
この操作は次の結果を返します。
[ { _id: { gameId: 'G2' }, gamescores: [ [ 10, 'PlayerA' ] ] }, { _id: { gameId: 'G1' }, gamescores: [ [ 1, 'PlayerD' ], [ 31, 'PlayerA' ], [ 33, 'PlayerB' ] ] } ]