Docs Menu
Docs Home
/
MongoDB 매뉴얼
/ / /

maxN(집계 누산기)

이 페이지의 내용

  • 정의
  • 구문
  • 행동
  • 제한 사항
  • 예제
$maxN

버전 5.2에 추가되었습니다.

그룹 내 최대값 n 개 요소의 애그리게이션을 반환합니다. 그룹에 n 개 미만의 요소가 포함된 경우 $maxN 은 그룹의 모든 요소를 반환합니다.

{
$maxN:
{
input: <expression>,
n: <expression>
}
}
  • input $maxN 에 대한 입력인 표현식을 지정합니다. 그룹의 각 요소에 대해 평가되며 $maxN 은 최대 n 값을 유지합니다.

  • n 그룹당 결과 수를 제한하고 n$group에 대한 _id 값에 의존하거나 상수인 양의 정수 표현식이어야 합니다.

  • $maxN null 및 누락된 값을 필터링합니다.

그룹에서 최대 n 문서를 반환하는 다음 애그리게이션을 가정해 보겠습니다.

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 플레이어 점수가 포함된 리터럴 문서를 생성합니다.

  • $groupgameId 기준으로 문서를 그룹화합니다. 이 예제에는 gameId가 단 하나(G1)만 있습니다.

  • PlayerD 점수가 누락되었고 PlayerE에 null score가 있습니다. 이 값은 모두 null로 간주됩니다.

  • maximumThreeScores 필드는 input : "$score" 이 포함된 $maxN 로 지정되고 배열로 반환됩니다.

  • scores 이 있는 문서가 3개뿐이므로 maxNn = 4 인 경우에도 최대 3개의 score 필드를 반환합니다.

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

$maxN } 축적자와 $topN 축적자는 모두 비슷한 결과를 달성할 수 있습니다.

일반적으로 다음과 같습니다.

$maxN 를 축적자로 사용할 수 있습니다.

$maxN애그리게이션 표현식으로 지원됩니다.

$maxNwindow operator 로 지원됩니다.

$maxN을 호출하는 집계 파이프라인에는 100MB 제한이 적용됩니다. 개별 그룹에 대해 이 제한을 초과하면 오류가 발생하면서 애그리게이션이 실패합니다.

다음 문서가 포함된 gamescores collection을 생각해 보세요.

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

$maxN 축적자를 사용하여 단일 게임에서 최대 3개의 점수를 찾을 수 있습니다.

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

예제 파이프라인:

  • $match를 사용하여 단일 gameId 결과를 필터링합니다. 이 경우에는 G1입니다.

  • $group을 사용하여 gameId를 기준으로 결과를 그룹화합니다. 이 경우에는 G1입니다.

  • input : ["$score","$playerId"] 을 사용하여 $maxN 에 입력되는 필드를 지정합니다.

  • $maxN 을 사용하여 n : 3 가 있는 G1 게임의 최대 3개 점수 요소를 반환합니다.

이 연산은 다음과 같은 결과를 반환합니다.

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

축적자를 사용하여 $maxN n 각 게임의 최대 점수를 찾을 수 있습니다.

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

예제 파이프라인:

  • $group을 사용하여 gameId를 기준으로 결과를 그룹화합니다.

  • $maxN 을 사용하여 n: 3 인 각 게임에 대해 최대 3개의 점수 요소를 반환합니다.

  • input: ["$score","$playerId"] 을 사용하여 $maxN 에 입력되는 필드를 지정합니다.

이 연산은 다음과 같은 결과를 반환합니다.

[
{
_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를 기준으로 결과를 그룹화합니다.

  • input : ["$score","$playerId"] 으로 $maxN 에 입력하는 필드를 지정합니다.

  • gameIdG2 이면 n 는 1이고, 그렇지 않으면 n 는 3입니다.

이 연산은 다음과 같은 결과를 반환합니다.

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

돌아가기

$max