$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:尋找總銷售額最低的商店
假設我們想要判斷第一個 Up 顧問公司內總銷售額最低的商店、執行查詢來擷取 First Up Consultants 公司內的檔、依總銷售額遞減順序排序檔,並傳回排序結果集中的最後一份檔。
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 }
}
]