Notitie
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen u aan te melden of de directory te wijzigen.
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen de mappen te wijzigen.
De $lastN accumulatoroperator retourneert de laatste N-waarden in een groep documenten voor een opgegeven expressie. Het is handig als u meerdere uiteindelijke waarden wilt ophalen uit een gesorteerde verzameling in plaats van alleen de enkele laatste waarde.
Syntaxis
{
$group: {
_id: < expression > ,
< field >: {
$lastN: {
n: < number >,
input: < expression >
}
}
}
}
Parameterwaarden
| Kenmerk | Description |
|---|---|
n |
Het aantal te retourneren waarden. Moet een positief geheel getal zijn. |
input |
De expressie waarmee het veld of de waarde wordt opgegeven die de laatste N-exemplaren retourneert. |
Voorbeelden
Bekijk dit voorbeelddocument uit de winkelverzameling.
{
"_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
}
]
}
]
}
Voorbeeld 1: Gebruik $lastN als accumulator om laatste twee promotie-gebeurtenissen per winkel te vinden
Als u de laatste twee promotiegebeurtenissen voor elke winkel wilt ophalen, voert u een query uit om promotiegebeurtenissen in oplopende volgorde van de begindatums te sorteren, de gesorteerde gebeurtenissen te groeperen op winkel en de laatste twee gebeurtenissen in elke winkel te retourneren.
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
}
}
}
}
])
De eerste twee resultaten die door deze query worden geretourneerd, zijn:
[
{
"_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
}
]
}
]
Voorbeeld 2: Gebruik $lastN als accumulator om de drie meest verkochte verkoopcategorieën te vinden
Als u de best verkopende verkoopcategorieën per winkel wilt ophalen, voert u een query uit om verkoopcategorieën in oplopende volgorde van het verkoopvolume te sorteren, de gesorteerde resultaten te groeperen op winkel en de laatste drie categorieën per winkel te retourneren.
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
}
}
}
}
])
De eerste twee resultaten die door deze query worden geretourneerd, zijn:
[
{
"_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
]
}
]
Voorbeeld 3: Gebruik $lastN operator als matrixexpressie om de laatste twee promotiegebeurtenissen op te halen
In het voorbeeld wordt het gebruik van de operator gedemonstreerd om de laatste of meest recente twee promotie-gebeurtenissen uit een winkel te vinden.
db.stores.aggregate([
{ $match: {"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"} },
{
$project: {
name: 1,
lastTwoPromotions: {
$lastN: {
input: "$promotionEvents",
n: 2
}
}
}
}
])
Deze query retourneert het volgende resultaat.
[
{
"_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
}
]
}
]
}
]