$bottom (aggregation accumulator)
定義
構文
{ $bottom: { sortBy: { <field1>: <sort order>, <field2>: <sort order> ... }, output: <expression> } }
フィールド | 必要性 | 説明 |
---|---|---|
sortBy | 必須 | |
出力 | 必須 | グループ内の各要素の出力を表し、任意の 式 にすることができます。 |
動作
NULL および欠損値
Consider the following aggregation that returns the bottom document from a group of scores:
$bottom
は null 値を除外しません。$bottom
は欠落値を 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: { $bottom: { output: [ "$playerId", "$score" ], sortBy: { "score": -1 } } } } } ] )
この例では、次のことが行われます。
$documents
は、プレイヤーのスコアを含むリテラル ドキュメントを作成します。$group
はドキュメントをgameId
でグループ化します。 この例ではgameId
、G1
は 1 つのみです。PlayerD
にはスコアがあり、PlayerE
には nullscore
があります。 これらの値は両方とも null と見なされます。playerId
フィールドとscore
フィールドはoutput : ["$playerId"," $score"]
として指定され、配列値として返されます。sortBy: { "score": -1 }
で並べ替え順序を指定します。PlayerD
andPlayerE
tied for the bottom element.PlayerD
is returned as the bottomscore
.複数の null 値に対してより明確な同点動作を実現するには、
sortBy
にフィールドを追加します。
[ { _id: 'G1', playerId: [ 'PlayerD', null ] } ]
制限事項
ウィンドウ関数と集計式のサポート
$bottom
は集計式としてサポートされていません。
$bottom
はwindow operator
としてサポートされています。
メモリ制限に関する考慮事項
$bottom
を呼び出す集計パイプラインには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 } ])
Find the Bottom Score
You can use the $bottom
accumulator to find the bottom score in a
single game.
db.gamescores.aggregate( [ { $match : { gameId : "G1" } }, { $group: { _id: "$gameId", playerId: { $bottom: { output: [ "$playerId", "$score" ], sortBy: { "score": -1 } } } } } ] )
サンプル パイプライン:
$match
を使用して結果を単一のgameId
でフィルタリングします。 この場合は、G1
ます。$group
を使用して結果をgameId
でグループ化します。 この場合は、G1
ます。を使用して
$bottom
output : ["$playerId"," $score"]
の出力するフィールドを指定します。スコアを降順で並べ替えるには、
sortBy: { "score": -1 }
を使用します。Uses
$bottom
to return the bottom score for the game.
この操作は次の結果を返します。
[ { _id: 'G1', playerId: [ 'PlayerD', 1 ] } ]
Finding the Bottom Score
Across Multiple Games
You can use the $bottom
accumulator to find the bottom score
in each game.
db.gamescores.aggregate( [ { $group: { _id: "$gameId", playerId: { $bottom: { output: [ "$playerId", "$score" ], sortBy: { "score": -1 } } } } } ] )
サンプル パイプライン:
$group
を使用して結果をgameId
でグループ化します。Uses
$bottom
to return the bottomscore
for each game.を使用して
$bottom
output : ["$playerId", "$score"]
の出力するフィールドを指定します。スコアを降順で並べ替えるには、
sortBy: { "score": -1 }
を使用します。
この操作は次の結果を返します。
[ { _id: 'G2', playerId: [ 'PlayerA', 10 ] }, { _id: 'G1', playerId: [ 'PlayerD', 1 ] } ]