Docs Menu

$maxN (aggregation accumulator)

$maxN

バージョン 5.2 で追加

Returns an aggregation of the maxmimum value n elements within a group. If the group contains fewer than n elements, $maxN returns all elements in the group.

{
$maxN:
{
input: <expression>,
n: <expression>
}
}
  • input specifies an expression that is the input to $maxN. It is evaluated for each element in the group and $maxN preserves the maximum n values.

  • n limits the number of results per group and n has to be a positive integral expression that is either a constant or depends on the _id value for $group.

$maxNBSON 比較の順序に従って入力データを比較し、適切な出力型を決定します。入力データに複数のデータ型が含まれている場合、$maxN 出力型は比較順序で最も高くなります。

  • $maxN filters out null and missing values.

Consider the following aggregation that returns the maximum n documents from a group:

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",
maximumThreeScores:
{
$maxN:
{
input: "$score",
n: 4
}
}
}
}
] )

この例では、次のことが行われます。

  • $documentsは、プレイヤーのスコアを含むリテラル ドキュメントを作成します。

  • $groupはドキュメントをgameIdでグループ化します。 この例ではgameIdG1は 1 つのみです。

  • PlayerDにはスコアがあり、 PlayerEには null scoreがあります。 これらの値は両方とも null と見なされます。

  • The maximumThreeScores field is specified as $maxN with input : "$score" and returned as an array.

  • Since there are only 3 documents with scores maxN returns the maximum 3 score fields even though n = 4.

[
{
_id: 'G1',
maximumThreeScores: [ 3, 2, 1 ]
}
]

Both $maxN and $topN accumulators can accomplish similar results.

In general:

  • $maxN has the advantage of finding maximum values in no particular sort order. If you want to know the maximum values for n documents use $maxN.

  • If guaranteing a particular sort order is a requirement use $topN.

  • 使用 $topN if you don't intend on sorting on the output values.

You can use $maxN as an accumulator.

$maxN is supported as an aggregation expression.

$maxN is supported as a window operator.

$maxNを呼び出す集計パイプラインには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 }
])

You can use the $maxN accumulator to find the maximum three scores in a single game.

db.gamescores.aggregate( [
{
$match : { gameId : "G1" }
},
{
$group:
{
_id: "$gameId",
maxThreeScores:
{
$maxN:
{
input: ["$score","$playerId"],
n:3
}
}
}
}
] )

サンプル パイプライン:

  • $matchを使用して結果を単一のgameIdでフィルタリングします。 この場合は、 G1ます。

  • $groupを使用して結果をgameIdでグループ化します。 この場合は、 G1ます。

  • Specifies the fields that are input for $maxN with input : ["$score","$playerId"].

  • Uses $maxN to return the maximum three score elements for the G1 game with n : 3.

この操作は次の結果を返します。

[
{
_id: 'G1',
maxThreeScores: [ [ 99, 'PlayerC' ], [ 33, 'PlayerB' ], [ 31, 'PlayerA' ] ]
}
]

You can use the $maxN accumulator to find the maximum n scores in each game.

db.gamescores.aggregate( [
{
$group:
{
_id: "$gameId",
maxScores:
{
$maxN:
{
input: ["$score","$playerId"],
n: 3
}
}
}
}
] )

サンプル パイプライン:

  • $groupを使用して結果をgameIdでグループ化します。

  • Uses $maxN to return the maximum three score elements for each game with n: 3.

  • Specifies the fields that are input for $maxN with input: ["$score","$playerId"].

この操作は次の結果を返します。

[
{
_id: 'G1',
maxScores: [ [ 99, 'PlayerC' ], [ 33, 'PlayerB' ], [ 31, 'PlayerA' ] ]
},
{
_id: 'G2',
maxScores: [ [ 80, 'PlayerD' ], [ 66, 'PlayerC' ], [ 14, 'PlayerB' ] ]
}
]

また、 nの値を動的に割り当てることもできます。 この例では、 $cond式はgameIdフィールドで使用されています。

db.gamescores.aggregate([
{
$group:
{
_id: {"gameId": "$gameId"},
gamescores:
{
$maxN:
{
input: ["$score","$playerId"],
n: { $cond: { if: {$eq: ["$gameId","G2"] }, then: 1, else: 3 } }
}
}
}
}
] )

サンプル パイプライン:

  • $groupを使用して結果をgameIdでグループ化します。

  • Specifies the fields that input for $maxN with input : ["$score","$playerId"].

  • gameIdG2の場合、 nは 1、それ以外の場合、 nは 3 になります。

この操作は次の結果を返します。

[
{ _id: { gameId: 'G2' }, gamescores: [ [ 80, 'PlayerD' ] ] },
{
_id: { gameId: 'G1' },
gamescores: [ [ 99, 'PlayerC' ], [ 33, 'PlayerB' ], [ 31, 'PlayerA' ] ]
}
]