연산자는 $bottom 쿼리에 지정된 하나 이상의 필드에서 문서를 정렬하고 필터링 조건과 일치하는 마지막 문서를 반환합니다.
문법
{
$bottom: {
output: [listOfFields],
sortBy: {
<fieldName>: < sortOrder >
}
}
}
매개 변수
| 매개 변수 | Description |
|---|---|
listOfFields |
결과 집합의 마지막 문서에서 반환할 필드 목록입니다. |
fieldName |
결과 집합을 정렬하는 데 사용할 필드입니다. |
sortOrder |
1 또는 -1. 1은 필드 값의 오름차순으로 정렬하는 것을 의미하고 -1 필드 값의 내림차순으로 정렬하는 것을 의미합니다. |
예시
스토어 컬렉션에서 이 샘플 문서를 고려합니다.
{
"_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: 총 판매액이 가장 낮은 매장 찾기
총 판매액이 가장 낮은 First Up 컨설턴트 회사 내에서 저장소를 확인하고, 쿼리를 실행하여 First Up 컨설턴트 회사 내에서 문서를 검색하고, 총 판매액의 내림차순으로 문서를 정렬하고, 정렬된 결과 집합의 마지막 문서를 반환하려고 합니다.
db.stores.aggregate([{
$match: {
company: {
$in: ["First Up Consultants"]
}
}
}, {
$group: {
_id: "$company",
bottomSales: {
$bottom: {
output: ["$company", "$sales"],
sortBy: {
"sales.revenue": -1
}
}
}
}
}])
이 쿼리는 다음 결과를 반환합니다.
[
{
"_id": "First Up Consultants",
"bottomSales": [
"First Up Consultants",
{
"totalSales": 119,
"salesByCategory": [
{
"categoryName": "Skirts",
"totalSales": 109
}
]
}
]
}]
예제 2: 판매가 가장 낮은 저장소당 범주 찾기
매장당 판매량이 가장 낮은 범주를 찾으려면 쿼리를 실행하여 여러 판매 범주가 있는 매장을 검색하고, 각 저장소 내의 총 판매액의 내림차순으로 범주를 정렬하고, 정렬된 결과 집합의 마지막 문서를 반환합니다.
db.stores.aggregate([{
$unwind: "$sales.salesByCategory"
},
{
$match: {
"sales.salesByCategory.totalSales": {
$exists: true
}
}
},
{
$group: {
_id: "$_id",
storeName: {
$first: "$name"
},
lowestCategory: {
$bottom: {
sortBy: {
"sales.salesByCategory.totalSales": 1
},
output: {
categoryName: "$sales.salesByCategory.categoryName",
totalSales: "$sales.salesByCategory.totalSales"
}
}
}
}
}
])
이 쿼리에서 반환된 처음 두 가지 결과는 다음과 같습니다.
[
{
"_id": "b1d86d1f-8705-4157-b64c-a9eda0df4921",
"storeName": "VanArsdel, Ltd. | Baby Products Haven - West Kingfort",
"lowestCategory": { "categoryName": "Baby Monitors", "totalSales": 49585 }
},
{
"_id": "22e6367e-8341-415f-9795-118d2b522abf",
"storeName": "Adatum Corporation | Outdoor Furniture Mart - Port Simone",
"lowestCategory": { "categoryName": "Outdoor Benches", "totalSales": 4976 }
}
]