AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
See how MongoDB 9.0 delivers up to 2x higher throughput.
MongoDB Branding Shape
Register now >
Docs Menu

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

サンプル パイプライン:

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

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

  • sortBy: { "imdb.rating": -1, title: 1 } を使用して映画の評価をランク付けし、title をタイブレークとして使用します。

  • output: [ "$title", "$imdb.rating" ] を使用して $bottomN によって返されるフィールドを指定します。

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

サンプル パイプライン:

  • $unwindを使用して各映画のgenres 配列を展開します。

  • $matchimdb.ratingを使用して、 値を含むドキュメントのみを保持します。

  • $group結果をジャンル別にグループ化するには、 を使用します。

  • output: [ "$title", "$imdb.rating" ] を使用して $bottomN によって返されるフィールドを指定します。

  • sortBy: { "imdb.rating": -1, title: 1 } を使用して映画の評価をランク付けし、title を決定的なタイブレークとして使用します。

  • $bottomN を使用して、n: 3 を持つ各ジャンルの下 3 つの映画を返します。

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

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

サンプル パイプライン:

  • $matchGを使用して、 、PG 、PG-13 、またはR のコンテンツ評価で、imdb.rating 値が含まれる映画のみを保持します。

  • $groupを使用して、結果を_id: { rated: "$rated" } のコンテンツ評価でグループ化します。

  • output: { title: "$title", rating: "$imdb.rating" } を使用して $bottomN によって返されるフィールドを指定します。

  • コンテンツ 評価が PG の場合、n は 3 です。それ以外の場合、n は 1 です。

  • sortBy: { "imdb.rating": -1 } を使用して、 IMDb スコアに基づいて最下位の n 映画を検索します。

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

このページを評価