AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。

$bottomN(アキュムレータ演算子)

$bottomN

バージョン5.2の新機能。

指定ソート順に従って、グループ内の最下位のn要素の集計を返します。 グループに含まれる要素がn未満の場合、 $bottomNはグループ内のすべての要素を返します。

注意

曖昧さ回避

このページでは、$bottomN をアキュムレータとして使用する場合について説明します。アキュムレータは、入力ドキュメントのグループ全体で集計値を返します。

また、$bottomN (window function) は、指定されたソート順に従って、特定のウィンドウ内のドキュメントから最下位の n 個の要素を返します。

{ $bottomN: { n: <expression>, output: <expression>, sortBy: { <field1>: <sort order>, <field2>: <sort order> ... } }}
フィールド
必要性
説明

n

必須

$bottomN がグループから返す要素の数。n を動的に設定する例については、グループキーに基づいて n を動的に設定する。 を参照してください。

output

必須

グループ内の各要素に対する出力を決定します。任意の式を指定できます。

sortBy

必須

各グループ内の要素のランキング順序を定義します。構文は、$sort に似ています。パイプライン出力のドキュメントの順序には影響しません。

  • $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には null scoreがあります。 これらの値は両方とも null と見なされます。

  • playerIdフィールドとscoreフィールドはoutput : ["$playerId"," $score"]として指定され、配列値として返されます。

  • sortBy: { "score" : -1 }により、null 値は返されたplayerId配列の末尾にソートされます。

[ { _id: "G1", playerId: [ [ "PlayerA", 1 ], [ "PlayerD", null ], [ "PlayerE", null ] ] }]

異なるタイプをソートする場合、順序を決定するために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 配置にこのデータセットをロードする方法の詳細については、「サンプル データセットのロード」を参照してください。サンプル データベースに何らかの変更を加えた場合は、今ページの例を実行するには、データベースを削除して再作成する必要がある可能性があります。

$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 } } } }] )

サンプル パイプライン:

  • $match を使用して、入力を imdb.rating 値を含む Short 個の映画に制限します。

  • $group を使用して、一致した映画を単一の Short グループに配置します。

  • sortBy: { "imdb.rating": -1, title: 1 } を使用して評価によって映画の順位を付け、title をタイブレーカーとして使用します。

  • $bottomN が output: [ "$title", "$imdb.rating" ] で返すフィールドを指定します。

  • $bottomN を使用して、n: 3 と一致する映画の下位 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 作の映画を返します。

  • $sort と $limit を使用して、最初の5つのジャンルをアルファベット順に表示します。

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 作品を検索します。

  • コンテンツ評価で結果をアルファベット順にソートします。

このページを評価