문서 메뉴

문서 홈애플리케이션 개발MongoDB 매뉴얼

minN(집계 누산기)

이 페이지의 내용

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

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

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

{
$minN:
{
input: <expression>,
n: <expression>
}
}
  • $minN 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",
minimumThreeScores:
{
$minN:
{
input: "$score",
n: 4
}
}
}
}
] )

이 예제에서는

  • $documents 플레이어 점수가 포함된 리터럴 문서를 생성합니다.

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

  • PlayerD 점수가 누락되었고 PlayerE 에 null score 이(가) 있습니다. 이 값은 모두 무시됩니다.

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

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

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

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

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

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

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

$minNwindow operator 로 지원됩니다.

$minN을 호출하는 집계 파이프라인에는 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 }
])

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

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

예제 파이프라인:

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

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

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

  • $minN 을 사용하여 n : 3G1 게임의 처음 세 개의 점수 요소를 반환합니다.

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

[
{
_id: 'G1',
minScores: [ [ 1, 'PlayerD' ], [ 31, 'PlayerA' ], [ 33, 'PlayerB' ] ]
}
]

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

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

예제 파이프라인:

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

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

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

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

[
{
_id: 'G2',
minScores: [ [ 10, 'PlayerA' ], [ 14, 'PlayerB' ], [ 66, 'PlayerC' ] ]
},
{
_id: 'G1',
minScores: [ [ 1, 'PlayerD' ], [ 31, 'PlayerA' ], [ 33, 'PlayerB' ] ]
}
]

n 의 값을 동적으로 할당할 수도 있습니다. 이 예제에서는 $cond 표현식이 gameId 필드에 사용됩니다.

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

예제 파이프라인:

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

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

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

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

[
{ _id: { gameId: 'G2' }, gamescores: [ [ 10, 'PlayerA' ] ] },
{
_id: { gameId: 'G1' },
gamescores: [ [ 1, 'PlayerD' ], [ 31, 'PlayerA' ], [ 33, 'PlayerB' ] ]
}
]
← $min (애그리게이션)