집계 파이프라인의 $sortByCount 단계는 지정된 식에 따라 문서를 그룹화한 다음, 각 그룹의 문서 수를 내림차순으로 정렬하는 데 사용됩니다. 이 $sortByCount 단계는 데이터 세트 내에서 가장 일반적인 값을 빠르게 식별하는 데 유용합니다.
문법
{
$sortByCount: <expression>
}
매개 변수
| 매개 변수 | Description |
|---|---|
expression |
이 구문은 문서를 그룹화하고 계산할 필드 또는 계산 식입니다. |
예시
스토어 컬렉션에서 이 샘플 문서를 고려합니다.
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
"location": {
"lat": -89.2384,
"lon": -46.4012
},
"staff": {
"totalStaff": {
"fullTime": 8,
"partTime": 20
}
},
"sales": {
"totalSales": 75670,
"salesByCategory": [
{
"categoryName": "Wine Accessories",
"totalSales": 34440
},
{
"categoryName": "Bitters",
"totalSales": 39496
},
{
"categoryName": "Rum",
"totalSales": 1734
}
]
},
"promotionEvents": [
{
"eventName": "Unbeatable Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 6,
"Day": 23
},
"endDate": {
"Year": 2024,
"Month": 7,
"Day": 2
}
},
"discounts": [
{
"categoryName": "Whiskey",
"discountPercentage": 7
},
{
"categoryName": "Bitters",
"discountPercentage": 15
},
{
"categoryName": "Brandy",
"discountPercentage": 8
},
{
"categoryName": "Sports Drinks",
"discountPercentage": 22
},
{
"categoryName": "Vodka",
"discountPercentage": 19
}
]
},
{
"eventName": "Steal of a Deal Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 29
}
},
"discounts": [
{
"categoryName": "Organic Wine",
"discountPercentage": 19
},
{
"categoryName": "White Wine",
"discountPercentage": 20
},
{
"categoryName": "Sparkling Wine",
"discountPercentage": 19
},
{
"categoryName": "Whiskey",
"discountPercentage": 17
},
{
"categoryName": "Vodka",
"discountPercentage": 23
}
]
}
]
}
예제 1: 이름별로 승격 이벤트 그룹화 및 내림차순으로 발생 횟수 계산
eventName 필드를 기준으로 그룹화하고 각 이벤트 이름의 발생 횟수를 계산하려면 결과를 카운트의 내림차순으로 정렬합니다.
db.stores.aggregate([
{ $unwind: "$promotionEvents" },
{ $sortByCount: "$promotionEvents.eventName" }
])
이 쿼리는 다음 결과를 반환합니다.
[
{ "_id": "Crazy Deal Days", "count": 4239 },
{ "_id": "Markdown Madness", "count": 2967 },
{ "_id": "Bargain Bonanza", "count": 2925 },
{ "_id": "Crazy Discount Days", "count": 2922 },
{ "_id": "Price Smash Spectacular", "count": 2915 },
{ "_id": "Super Saver Spectacular", "count": 2900 },
{ "_id": "Crazy Markdown Madness", "count": 2899 },
{ "_id": "Price Cut Carnival", "count": 2868 },
{ "_id": "Grand Bargain Bash", "count": 2849 },
{ "_id": "Bargain Blitz Bash", "count": 2843 },
{ "_id": "Grand Savings Gala", "count": 2826 },
{ "_id": "Super Saver Fiesta", "count": 1551 },
{ "_id": "Major Deal Days", "count": 1548 },
{ "_id": "Price Slash Carnival", "count": 1535 },
{ "_id": "Super Discount Days", "count": 1533 },
{ "_id": "Big Deal Bonanza", "count": 1533 },
{ "_id": "Incredible Savings Showcase", "count": 1531 },
{ "_id": "Unbeatable Savings Spectacular", "count": 1518 },
{ "_id": "Fantastic Deal Days", "count": 1511 },
{ "_id": "Flash Bargain Frenzy", "count": 1504 }
]
이 파이프라인은 다음을 수행합니다.
- $unwind를 사용하여 입력 문서에서 promotionEvents 배열 필드를 분해합니다.
- $sortByCount를 사용하여 eventName 필드를 기준으로 그룹화하고, 각 이벤트 이름의 발생 횟수를 계산하여 발생 횟수 기준의 내림차순으로 결과를 정렬합니다.