适用对象:
MongoDB vCore
$lastN
累加器运算符返回指定表达式的一组文档中的最后 N 个值。 当需要从排序的集合中检索多个最终值,而不仅仅是单个最后一个值时,它很有用。
语法
累加器运算符的 $lastN
语法如下所示:
{
$group: {
_id: <expression>,
<field>: {
$lastN: {
input: <expression>,
n: <number>
}
}
}
}
参数
DESCRIPTION | |
---|---|
input |
指定要返回最后 N 个匹配项的字段或值的表达式。 |
n |
要返回的值数。 必须是正整数。 |
示例:
让我们了解 stores
数据集中的示例 json 的用法。
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
"sales": {
"totalSales": 151864,
"salesByCategory": [
{
"categoryName": "Sound Bars",
"totalSales": 2120
},
{
"categoryName": "Home Theater Projectors",
"totalSales": 45004
},
{
"categoryName": "Game Controllers",
"totalSales": 43522
},
{
"categoryName": "Remote Controls",
"totalSales": 28946
},
{
"categoryName": "VR Games",
"totalSales": 32272
}
]
},
"promotionEvents": [
{
"eventName": "Massive Markdown Mania",
"promotionalDates": {
"startDate": {
"Year": 2023,
"Month": 6,
"Day": 29
}
}
},
{
"eventName": "Fantastic Deal Days",
"promotionalDates": {
"startDate": {
"Year": 2023,
"Month": 9,
"Day": 27
}
}
},
{
"eventName": "Major Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
}
}
}
]
}
示例 1:按日期获取最后两个促销事件
检索每个商店的最新两个促销事件。
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:获取前三大销售类别
查找每个商店最畅销的三大类别。
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 ]
},
.
.
.
]