$sum

Der $sum Operator berechnet die Summe numerischer Werte eines Felds oder Ausdrucks, die einem Filterkriterium entsprechen.

Syntax

{
  $sum: <field or expression>
}

Die Parameter

Parameter Description
<field or expression> Das Feld oder der Ausdruck, von dem die Summe berechnet werden soll. Dies kann ein Feld, ein numerischer Wert oder ein Ausdruck sein.

Examples

Betrachten Sie dieses Beispieldokument aus der Stores-Sammlung.

{
    "_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
                }
            ]
        }
    ]
}

Beispiel 1: Verwenden $sum in $group

So berechnen Sie den Gesamtrabattprozentsatz pro Kategorie für alle Werbeaktionen im Jahr 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
        }
    }
])

Diese Abfrage gibt die folgenden Ergebnisse zurück:

[
    {
        "categoryName": "Glass Frames",
        "totalDiscountIn2023": 25
    },
    {
        "categoryName": "Picture Hanging Supplies",
        "totalDiscountIn2023": 14
    }
]

Beispiel 2: Verwenden $sum in $bucket

So rufen Sie den Gesamtumsatz innerhalb definierter Vertriebsgrenzen ab:

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
        }
    }
}])

Diese Abfrage gibt die folgenden Ergebnisse zurück:

[
    {
        "_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
    }
]

Beispiel 3: Verwenden in $sum$setWindowFields

So berechnen Sie den Gesamtrabatt pro Verkaufskategorie für Werbeaktionen im Jahr 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
    }
])

Diese Abfrage gibt die folgenden Ergebnisse zurück:

[
    {
        "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
    }
]