$lastN 累加器運算子會針對指定的運算式傳回文件群組中的最後 N 個值。 當您需要從已排序的集合中擷取多個最終值,而不是僅擷取最後一個值時,這將有其效用。
語法
{
$group: {
_id: < expression > ,
< field >: {
$lastN: {
n: < number >,
input: < expression >
}
}
}
}
參數
| 參數 | Description |
|---|---|
n |
要傳回的值數目。 必須是正整數。 |
input |
指定要傳回最後 N 次出現的欄位或值的運算式。 |
範例
請參考商店集合中的此範例檔。
{
"_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:使用 $lastN 作為累積器,依商店尋找最後兩個促銷事件
若要擷取每個商店的最後兩個促銷事件,請執行查詢以遞增順序排序促銷事件,依存放區分組排序事件,並傳回每個存放區內的最後兩個事件。
db.stores.aggregate([{
$unwind: "$promotionEvents"
},
{
$sort: {
"promotionEvents.promotionalDates.startDate.Year": 1,
"promotionEvents.promotionalDates.startDate.Month": 1,
"promotionEvents.promotionalDates.startDate.Day": 1
}
},
{
$group: {
_id: "$_id",
storeName: {
$last: "$name"
},
lastTwoPromotions: {
$lastN: {
input: "$promotionEvents.eventName",
n: 2
}
},
lastTwoPromotionDates: {
$lastN: {
input: "$promotionEvents.promotionalDates.startDate",
n: 2
}
}
}
}
])
此查詢傳回的前兩個結果是:
[
{
"_id": "e28fff9b-a8fb-4ac9-bb37-dea60d2a7d32",
"storeName": "Lakeshore Retail | Outdoor Furniture Collection - Erdmanside",
"lastTwoPromotions": [
"Big Bargain Bash",
"Spectacular Savings Showcase"
],
"lastTwoPromotionDates": [
{
"Year": 2024,
"Month": 9,
"Day": 21
},
{
"Year": 2024,
"Month": 6,
"Day": 23
}
]
},
{
"_id": "1bec7539-dc75-4f7e-b4e8-afdf8ff2f234",
"storeName": "Adatum Corporation | Health Food Market - East Karina",
"lastTwoPromotions": [
"Price Slash Spectacular",
"Spectacular Savings Showcase"
],
"lastTwoPromotionDates": [
{
"Year": 2024,
"Month": 9,
"Day": 21
},
{
"Year": 2024,
"Month": 6,
"Day": 23
}
]
}
]
範例 2:使用$lastN作為累積器來尋找三大銷售類別的銷售
若要擷取每個商店最高的銷售銷售類別,請執行查詢,依銷售量遞增順序排序銷售類別、依商店分組排序結果,並傳回每個商店的最後三個類別。
db.stores.aggregate([{
$unwind: "$sales.salesByCategory"
},
{
$sort: {
"sales.salesByCategory.totalSales": 1
}
},
{
$group: {
_id: "$_id",
storeName: {
$last: "$name"
},
top3Categories: {
$lastN: {
input: "$sales.salesByCategory.categoryName",
n: 3
}
},
top3SalesAmounts: {
$lastN: {
input: "$sales.salesByCategory.totalSales",
n: 3
}
}
}
}
])
此查詢傳回的前兩個結果是:
[
{
"_id": "22e6367e-8341-415f-9795-118d2b522abf",
"storeName": "Adatum Corporation | Outdoor Furniture Mart - Port Simone",
"top3Categories": [
"Outdoor Benches"
],
"top3SalesAmounts": [
4976
]
},
{
"_id": "a00a3ccd-49a2-4e43-b0d9-e56b96113ed0",
"storeName": "Wide World Importers | Smart Home Deals - Marcuschester",
"top3Categories": [
"Smart Thermostats",
"Smart Plugs"
],
"top3SalesAmounts": [
38696,
633
]
}
]
範例 3:使用 $lastN 運算符作為 array-expression 來取得最後兩個升級事件
此範例示範運算符的使用方式,從商店尋找最後或最近兩個促銷事件。
db.stores.aggregate([
{ $match: {"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"} },
{
$project: {
name: 1,
lastTwoPromotions: {
$lastN: {
input: "$promotionEvents",
n: 2
}
}
}
}
])
此查詢會傳回下列結果。
[
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
"lastTwoPromotions": [
{
"eventName": "Grand Deal Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 6,
"Day": 23
},
"endDate": {
"Year": 2024,
"Month": 6,
"Day": 30
}
},
"discounts": [
{
"categoryName": "Remote Controls",
"discountPercentage": 7
},
{
"categoryName": "Televisions",
"discountPercentage": 11
},
{
"categoryName": "Business Projectors",
"discountPercentage": 13
},
{
"categoryName": "Laser Projectors",
"discountPercentage": 6
},
{
"categoryName": "Projectors",
"discountPercentage": 6
},
{
"categoryName": "Projector Screens",
"discountPercentage": 24
}
]
},
{
"eventName": "Major Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 30
}
},
"discounts": [
{
"categoryName": "Sound Bars",
"discountPercentage": 9
},
{
"categoryName": "VR Games",
"discountPercentage": 7
},
{
"categoryName": "Xbox Games",
"discountPercentage": 25
},
{
"categoryName": "Projector Accessories",
"discountPercentage": 18
},
{
"categoryName": "Mobile Games",
"discountPercentage": 8
},
{
"categoryName": "Projector Cases",
"discountPercentage": 22
}
]
}
]
}
]