Kommentar
Åtkomst till den här sidan kräver auktorisering. Du kan prova att logga in eller ändra kataloger.
Åtkomst till den här sidan kräver auktorisering. Du kan prova att ändra kataloger.
Operatorn $sum beräknar summan av numeriska värden för ett fält eller uttryck som matchar ett filtreringsvillkor.
Syntax
{
$sum: <field or expression>
}
Parameterar
| Parameter | Description |
|---|---|
<field or expression> |
Fältet eller uttrycket för att beräkna summan av. Det kan vara ett fält, ett numeriskt värde eller ett uttryck. |
Examples
Överväg det här exempeldokumentet från butiksamlingen.
{
"_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
}
]
}
]
}
Exempel 1: Använda $sum i $group
Så här beräknar du total rabattprocent per kategori för alla kampanjhändelser under 2023:
db.stores.aggregate([{
$unwind: "$promotionEvents"
},
{
$unwind: "$promotionEvents.discounts"
},
{
$match: {
"promotionEvents.promotionalDates.startDate.Year": 2023
}
},
{
$group: {
_id: "$promotionEvents.discounts.categoryName",
totalDiscountIn2023: {
$sum: "$promotionEvents.discounts.discountPercentage"
}
}
},
{
$project: {
_id: 0,
categoryName: "$_id",
totalDiscountIn2023: 1
}
}
])
Den här frågan returnerar följande resultat:
[
{
"categoryName": "Glass Frames",
"totalDiscountIn2023": 25
},
{
"categoryName": "Picture Hanging Supplies",
"totalDiscountIn2023": 14
}
]
Exempel 2: Använda $sum i $bucket
Så här hämtar du den totala försäljningen inom definierade försäljningsgränser:
db.stores.aggregate([{
$bucket: {
groupBy: "$sales.totalSales", // Field to group by
boundaries: [0, 10000, 20000, 50000, 100000], // Sales ranges
default: "Other", // Default bucket for values outside the defined ranges
output: {
totalSalesSum: {
$sum: "$sales.totalSales"
}, // Sum the sales for each bucket
count: {
$sum: 1
} // Count the number of documents in each bucket
}
}
}])
Den här frågan returnerar följande resultat:
[
{
"_id": "Other",
"totalSalesSum": 454981695,
"count": 3001
},
{
"_id": 0,
"totalSalesSum": 20033725,
"count": 3903
},
{
"_id": 10000,
"totalSalesSum": 71303703,
"count": 4712
},
{
"_id": 50000,
"totalSalesSum": 756168541,
"count": 11025
},
{
"_id": 20000,
"totalSalesSum": 678976078,
"count": 18861
}
]
Exempel 3: Använda $sum i $setWindowFields
Så här beräknar du den totala rabatten per försäljningskategori för kampanjer 2023:
db.stores.aggregate([{
$unwind: "$promotionEvents"
},
{
$unwind: "$promotionEvents.discounts"
},
{
$match: {
"promotionEvents.promotionalDates.startDate.Year": 2023
}
},
{
$setWindowFields: {
partitionBy: "$promotionEvents.discounts.categoryName", // Group by categoryName
output: {
totalDiscountIn2023: {
$sum: "$promotionEvents.discounts.discountPercentage", // Sum discount percentage
window: {
documents: ["unbounded", "unbounded"] // Aggregate over all documents in the partition
}
}
}
}
},
{
$project: {
_id: 0,
categoryName: "$promotionEvents.discounts.categoryName",
discountPercentage: "$promotionEvents.discounts.discountPercentage",
totalDiscountIn2023: 1
}
},
{
$limit: 5
}
])
Den här frågan returnerar följande resultat:
[
{
"totalDiscountIn2023": 1106,
"categoryName": "2-in-1 Laptops",
"discountPercentage": 25
},
{
"totalDiscountIn2023": 1106,
"categoryName": "2-in-1 Laptops",
"discountPercentage": 22
},
{
"totalDiscountIn2023": 1106,
"categoryName": "2-in-1 Laptops",
"discountPercentage": 19
},
{
"totalDiscountIn2023": 1106,
"categoryName": "2-in-1 Laptops",
"discountPercentage": 17
},
{
"totalDiscountIn2023": 1106,
"categoryName": "2-in-1 Laptops",
"discountPercentage": 10
}
]