定義
$bottomNバージョン5.2の新機能。
指定ソート順に従って、グループ内の最下位の
n要素の集計を返します。 グループに含まれる要素がn未満の場合、$bottomNはグループ内のすべての要素を返します。
注意
曖昧さ回避
このページでは、$bottomN をアキュムレータとして使用する場合について説明します。アキュムレータは、入力ドキュメントのグループ全体で集計値を返します。
bottomN は、これらの他のコンテキストでも使用できます。
指定されたソート順に従って、
$bottomN (window function)は特定のウィンドウ内のドキュメントから最下位のn個の要素を返します。$bottomN (expression operator)は、指定ソート順に従って、入力値の配列から最下位のn個の要素を返します。
構文
{ $bottomN: { n: <expression>, output: <expression>, sortBy: { <field1>: <sort order>, <field2>: <sort order> ... } } }
オプション
フィールド | 必要性 | 説明 |
|---|---|---|
| 必須 | The number of elements that |
| 必須 | グループ内の各要素に対する出力を決定します。任意の式を指定できます。 |
| 必須 | 各グループ内の要素のランキング順序を定義します。構文は、 |
動作
NULL および欠損値
$bottomNは、null 値をフィルタリングで除外しません。$bottomNは、出力に保持される欠落値を null に変換します。
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", playerId: { $bottomN: { output: [ "$playerId", "$score" ], sortBy: { "score": -1 }, n: 3 } } } } ] )
この例では、次のことが行われます。
$documentsは、プレイヤーのスコアを含むリテラル ドキュメントを作成します。$groupはドキュメントをgameIdでグループ化します。 この例ではgameId、G1は 1 つのみです。PlayerDはスコアが欠落しており、PlayerEには nullscoreがあります。 これらの値は両方とも null と見なされます。playerIdフィールドとscoreフィールドはoutput : ["$playerId"," $score"]として指定され、配列値として返されます。sortBy: { "score" : -1 }により、null 値は返されたplayerId配列の末尾にソートされます。
[ { _id: "G1", playerId: [ [ "PlayerA", 1 ], [ "PlayerD", null ], [ "PlayerE", null ] ] } ]
BSON データ型ソート順
異なるタイプをソートする場合、順序を決定するためにBSON データ型の順序が使用されます。 例として、値が string と数値で構成されるコレクションを考えてみましょう。
昇順ソートでは、string 値は数値の後にソートされます。
降順ソートでは、string 値が数値の前にソートされます。
db.aggregate( [ { $documents: [ { playerId: "PlayerA", gameId: "G1", score: 1 }, { playerId: "PlayerB", gameId: "G1", score: "2" }, { playerId: "PlayerC", gameId: "G1", score: "" } ] }, { $group: { _id: "$gameId", playerId: { $bottomN: { output: ["$playerId","$score"], sortBy: {"score": -1}, n: 3 } } } } ] )
この例では、次のことが行われます。
PlayerAは整数スコアを持ちます。PlayerBは string"2"スコアを持ちます。PlayerCには空のstringスコアがあります。
並べ替えは{ "score" : -1 }の降順であるため、string リテラル値はPlayerAの数値スコアより前にソートされます。
[ { _id: "G1", playerId: [ [ "PlayerB", "2" ], [ "PlayerC", "" ], [ "PlayerA", 1 ] ] } ]
例
このページの例では、sample_mflix データセット のデータを使用しています。自己管理型 MongoDB 配置にこのデータセットをロードする方法の詳細については、「サンプル データセットのロード」を参照してください。サンプル データベースに何らかの変更を加えた場合は、今ページの例を実行するには、データベースを削除して再作成する必要がある可能性があります。
1つのジャンルで評価が最も低い映画3本を検索
$bottomN アキュムレータを使用すると、Short ジャンルの中で評価が最も低い映画3本を検索できます。
db.movies.aggregate( [ { $match: { genres: "Short", "imdb.rating": { $exists: true } } }, { $group: { _id: "Short", lowestRatedMovies: { $bottomN: { output: [ "$title", "$imdb.rating" ], sortBy: { "imdb.rating": -1, title: 1 }, n: 3 } } } } ] )
サンプル パイプライン:
各ジャンルの最低評価映画を探す
$bottomN アキュムレータを使用して、各ジャンルの評価が最も低い映画を検索できます。
db.movies.aggregate( [ { $unwind: "$genres" }, { $match: { "imdb.rating": { $exists: true } } }, { $group: { _id: "$genres", lowestRatedMovie: { $bottomN: { output: [ "$title", "$imdb.rating" ], sortBy: { "imdb.rating": -1, title: 1 }, n: 3 } } } }, { $sort: { _id: 1 } }, { $limit: 5 } ] )
サンプル パイプライン:
各映画の
genres配列を拡張するために$unwindを使用します。$matchを使用して、imdb.rating値を含むドキュメントのみを保持します。結果をジャンル別にグループ化するために、
$groupを使用します。$bottomNがoutput: [ "$title", "$imdb.rating" ]で返すフィールドを指定します。sortBy: { "imdb.rating": -1, title: 1 }を使用して評価による映画の順付けを行い、titleを決定的なタイブレーカーとして使用します。$bottomNを使用して、n: 3で各ジャンルの下位 3 作の映画を返します。
グループキーに基づいて n を動的に設定する
n の値をダイナミックに割り当てることができます。次の例では、$cond 式を使用して、コンテンツの評価に基づいて異なる数の映画を返します。
db.movies.aggregate([ { $match: { rated: { $in: [ "G", "PG", "PG-13", "R" ] }, "imdb.rating": { $exists: true } } }, { $group: { _id: { rated: "$rated" }, movies: { $bottomN: { output: { title: "$title", rating: "$imdb.rating" }, n: { $cond: { if: { $eq: [ "$rated", "PG" ] }, then: 3, else: 1 } }, sortBy: { "imdb.rating": -1 } } } } }, { $sort: { "_id.rated": 1 } } ] )
サンプル パイプライン:
$matchを使用して、G、PG、PG-13、またはRのコンテンツ レーティングを持ち、imdb.ratingの値を含む映画のみを保持します。_id: { rated: "$rated" }を使用して、$groupでコンテンツの評価に基づいて結果をグループ化します。$bottomNがoutput: { title: "$title", rating: "$imdb.rating" }で返すフィールドを指定します。コンテンツの評価が
PGの場合、nは 3 です。それ以外の場合、nは 1 です。sortBy: { "imdb.rating": -1 }を使用して、IMDb スコアに基づいて下位n作品を検索します。コンテンツ評価で結果をアルファベット順にソートします。