AI 에이전트의 경우: 문서 인덱스는 https://www.mongodb.com/ko-kr/docs/llms.txt에서 사용할 수 있으며, 모든 페이지의 마크다운 버전은 어떤 URL 경로에 .md를 추가하여 사용할 수 있습니다.
Docs Menu

순위(창 함수)(표현식 연산자)

버전 5.0의 신규 항목입니다.

$rank

$setWindowFields 단계 파티션의 다른 문서를 기준으로 문서 위치(순위라고 함)를 반환합니다 .

$setWindowFields 단계의 sortBy 필드 값은 문서 순위를 결정합니다. $rank 연산자와 함께 사용되는 경우 sortBy는 하나의 필드만 값으로 취할 수 있습니다. MongoDB가 서로 다른 유형의 필드를 비교하는 방법에 대해 자세히 알아보려면 BSON 비교 순서를 참조하세요.

If multiple documents occupy the same rank, $rank places the document with the subsequent value at a rank with a gap (see Behavior).

$rank is only available in the $setWindowFields stage.

$rank 구문:

{ $rank: { } }

$rank 어떤 매개변수도 허용하지 않습니다.

$rank and $denseRank differ in how they rank duplicate sortBy field values. For example, with sortBy field values of 7, 9, 9, and 10:

  • $denseRank 값의 순위를 1, 2, 2 및 3으로 지정합니다. 중복된 9 값의 순위는 2이고 10의 순위는 3입니다. 순위에는 격차가 없습니다.

  • $rank 값의 순위를 1, 2, 2 및 4로 지정합니다. 중복된 9 값의 순위는 2이고 10의 순위는 4입니다. 3위와 순위가 차이가 납니다.

sortBy 필드에 null 값이 있는 문서 또는 sortBy 필드가 누락된 문서에는 BSON 비교 순서에 따라 순위가 지정됩니다.

중복 값, Null 또는 누락된 데이터가 포함된 파티션 순위 지정의 예시를 참조하세요.

캘리포니아주(CA)와 워싱턴주(WA)의 케이크 판매량이 포함된 cakeSales collection을 생성합니다.

db.cakeSales.insertMany( [
{ _id: 0, type: "chocolate", orderDate: new Date("2020-05-18T14:10:30Z"),
state: "CA", price: 13, quantity: 120 },
{ _id: 1, type: "chocolate", orderDate: new Date("2021-03-20T11:30:05Z"),
state: "WA", price: 14, quantity: 140 },
{ _id: 2, type: "vanilla", orderDate: new Date("2021-01-11T06:31:15Z"),
state: "CA", price: 12, quantity: 145 },
{ _id: 3, type: "vanilla", orderDate: new Date("2020-02-08T13:13:23Z"),
state: "WA", price: 13, quantity: 104 },
{ _id: 4, type: "strawberry", orderDate: new Date("2019-05-18T16:09:01Z"),
state: "CA", price: 41, quantity: 162 },
{ _id: 5, type: "strawberry", orderDate: new Date("2019-01-08T06:12:03Z"),
state: "WA", price: 43, quantity: 134 }
] )

This example uses $rank in the $setWindowFields stage to output the quantity rank of the cake sales for each state:

db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { quantity: -1 },
output: {
rankQuantityForState: {
$rank: {}
}
}
}
}
] )

예시:

  • partitionBy: "$state" 는 collection의 문서를 state로 파티셔닝합니다. CA 및 WA에 대한 파티션이 있습니다.

  • sortBy: { quantity: -1 } 각 파티션의 문서를 quantity 별로 내림차순(-1)으로 정렬하므로 가장 높은 quantity이(가) 첫 번째입니다.

  • output sets the rankQuantityForState field to the quantity rank using $rank, as shown in the following results.
{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"),
"state" : "CA", "price" : 41, "quantity" : 162, "rankQuantityForState" : 1 }
{ "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"),
"state" : "CA", "price" : 12, "quantity" : 145, "rankQuantityForState" : 2 }
{ "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"),
"state" : "CA", "price" : 13, "quantity" : 120, "rankQuantityForState" : 3 }
{ "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"),
"state" : "WA", "price" : 14, "quantity" : 140, "rankQuantityForState" : 1 }
{ "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"),
"state" : "WA", "price" : 43, "quantity" : 134, "rankQuantityForState" : 2 }
{ "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"),
"state" : "WA", "price" : 13, "quantity" : 104, "rankQuantityForState" : 3 }

This example shows how to use dates with $rank in the $setWindowFields stage to output the orderDate rank of the cake sales for each state:

db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { orderDate: 1 },
output: {
rankOrderDateForState: {
$rank: {}
}
}
}
}
] )

예시:

  • partitionBy: "$state" 는 collection의 문서를 state로 파티셔닝합니다. CA 및 WA에 대한 파티션이 있습니다.

  • sortBy: { orderDate: 1 } 각 파티션의 문서를 orderDate을 기준으로 오름차순(1)으로 정렬하므로, 가장 이른 orderDate이 첫 번째가 됩니다.

  • output sets the rankOrderDateForState field to the orderDate rank using $rank, as shown in the following results.
{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"),
"state" : "CA", "price" : 41, "quantity" : 162, "rankOrderDateForState" : 1 }
{ "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"),
"state" : "CA", "price" : 13, "quantity" : 120, "rankOrderDateForState" : 2 }
{ "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"),
"state" : "CA", "price" : 12, "quantity" : 145, "rankOrderDateForState" : 3 }
{ "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"),
"state" : "WA", "price" : 43, "quantity" : 134, "rankOrderDateForState" : 1 }
{ "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"),
"state" : "WA", "price" : 13, "quantity" : 104, "rankOrderDateForState" : 2 }
{ "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"),
"state" : "WA", "price" : 14, "quantity" : 140, "rankOrderDateForState" : 3 }

다음에서 cakeSalesWithDuplicates 컬렉션을 만듭니다.

  • 케이크 판매는 캘리포니아주(CA)와 워싱턴주(WA)에서 이루어집니다.

  • 문서 6~8은 문서 5와 동일한 quantity 및 state을(를) 갖습니다.

  • 문서 9에는 문서 4와 동일한 quantity 및 state이(가) 있습니다.

  • 문서 10에는 null quantity이(가) 있습니다.

  • 문서 11에 quantity이(가) 누락되었습니다.

db.cakeSalesWithDuplicates.insertMany( [
{ _id: 0, type: "chocolate", orderDate: new Date("2020-05-18T14:10:30Z"),
state: "CA", price: 13, quantity: 120 },
{ _id: 1, type: "chocolate", orderDate: new Date("2021-03-20T11:30:05Z"),
state: "WA", price: 14, quantity: 140 },
{ _id: 2, type: "vanilla", orderDate: new Date("2021-01-11T06:31:15Z"),
state: "CA", price: 12, quantity: 145 },
{ _id: 3, type: "vanilla", orderDate: new Date("2020-02-08T13:13:23Z"),
state: "WA", price: 13, quantity: 104 },
{ _id: 4, type: "strawberry", orderDate: new Date("2019-05-18T16:09:01Z"),
state: "CA", price: 41, quantity: 162 },
{ _id: 5, type: "strawberry", orderDate: new Date("2019-01-08T06:12:03Z"),
state: "WA", price: 43, quantity: 134 },
{ _id: 6, type: "strawberry", orderDate: new Date("2020-01-08T06:12:03Z"),
state: "WA", price: 41, quantity: 134 },
{ _id: 7, type: "strawberry", orderDate: new Date("2020-01-01T06:12:03Z"),
state: "WA", price: 34, quantity: 134 },
{ _id: 8, type: "strawberry", orderDate: new Date("2020-01-02T06:12:03Z"),
state: "WA", price: 40, quantity: 134 },
{ _id: 9, type: "strawberry", orderDate: new Date("2020-05-11T16:09:01Z"),
state: "CA", price: 39, quantity: 162 },
{ _id: 10, type: "strawberry", orderDate: new Date("2020-05-11T16:09:01Z"),
state: "CA", price: 39, quantity: null },
{ _id: 11, type: "strawberry", orderDate: new Date("2020-05-11T16:09:01Z"),
state: "CA", price: 39 }
] )

This example uses $rank in the $setWindowFields stage to output the quantity rank from the cakeSalesWithDuplicates collection for each state:

db.cakeSalesWithDuplicates.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { quantity: -1 },
output: {
rankQuantityForState: {
$rank: {}
}
}
}
}
] )

예시:

  • partitionBy: "$state" 는 collection의 문서를 state로 파티셔닝합니다. CA 및 WA에 대한 파티션이 있습니다.

  • sortBy: { quantity: -1 } 각 파티션의 문서를 quantity 별로 내림차순(-1)으로 정렬하므로 가장 높은 quantity이(가) 첫 번째입니다.

  • output sets the rankOrderDateForState field to the quantity rank using $rank.

다음 예시 출력은 다음과 같습니다.

  • 동일한 quantity 및 state을(를) 가진 문서는 순위가 동일합니다. 문서의 순위가 동일하면 해당 순위와 다음 순위 사이에 간격이 있습니다.

  • null quantity이(가) 있는 문서와 누락된 quantity이(가) 있는 문서가 CA 파티션의 출력에서 가장 낮은 순위로 평가됩니다. 이 정렬은 BSON 비교 순서의 결과로 이 예시에서는 숫자 값 다음에 null 및 누락된 값을 정렬합니다.

{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"),
"state" : "CA", "price" : 41, "quantity" : 162, "rankQuantityForState" : 1 }
{ "_id" : 9, "type" : "strawberry", "orderDate" : ISODate("2020-05-11T16:09:01Z"),
"state" : "CA", "price" : 39, "quantity" : 162, "rankQuantityForState" : 1 }
{ "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"),
"state" : "CA", "price" : 12, "quantity" : 145, "rankQuantityForState" : 3 }
{ "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"),
"state" : "CA", "price" : 13, "quantity" : 120, "rankQuantityForState" : 4 }
{ "_id" : 10, "type" : "strawberry", "orderDate" : ISODate("2020-05-11T16:09:01Z"),
"state" : "CA", "price" : 39, "quantity" : null, "rankQuantityForState" : 5 }
{ "_id" : 11, "type" : "strawberry", "orderDate" : ISODate("2020-05-11T16:09:01Z"),
"state" : "CA", "price" : 39, "rankQuantityForState" : 6 }
{ "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"),
"state" : "WA", "price" : 14, "quantity" : 140, "rankQuantityForState" : 1 }
{ "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"),
"state" : "WA", "price" : 43, "quantity" : 134, "rankQuantityForState" : 2 }
{ "_id" : 6, "type" : "strawberry", "orderDate" : ISODate("2020-01-08T06:12:03Z"),
"state" : "WA", "price" : 41, "quantity" : 134, "rankQuantityForState" : 2 }
{ "_id" : 7, "type" : "strawberry", "orderDate" : ISODate("2020-01-01T06:12:03Z"),
"state" : "WA", "price" : 34, "quantity" : 134, "rankQuantityForState" : 2 }
{ "_id" : 8, "type" : "strawberry", "orderDate" : ISODate("2020-01-02T06:12:03Z"),
"state" : "WA", "price" : 40, "quantity" : 134, "rankQuantityForState" : 2 }
{ "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"),
"state" : "WA", "price" : 13, "quantity" : 104, "rankQuantityForState" : 6 }
이 페이지 평가하기

이 페이지의 내용