$stddevpop

Der $stddevpop Operator berechnet die Standardabweichung der angegebenen Werte. Der Operator kann nur die Standardabweichung numerischer Werte berechnen.

Syntax

{
  $stddevpop: {fieldName}
}

Die Parameter

Parameter Description
fieldName Das Feld, dessen Werte zum Berechnen der Standardabweichung verwendet werden

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 : Berechnen der Standardabweichung des Gesamtumsatzes

Um die Standardabweichung des Gesamtumsatzes für alle Verkaufskategorien für Geschäfte zu berechnen, die zu "Fourth Coffee" gehören, filtern Sie zuerst nach dem Firmenfeld, und berechnen Sie dann den Gesamtumsatz in allen resultierenden Stores mit stddevpop und geben das aggregierte Ergebnis zurück.

db.stores.aggregate([{
    $match: {
        company: "Fourth Coffee"
    }
}, {
    $group: {
        _id: "$company",
        stdDev: {
            $stdDevPop: "$sales.totalSales"
        }
    }
}])[{
    _id: 'Fourth Coffee',
    stdDev: 0
}]

Diese Abfrage gibt das folgende Ergebnis zurück:

[
  {
      "_id": "Fourth Coffee",
      "stdDev": 39133.27057120701
  }
]

Beispiel 2: Berechnen der Standardabweichung eines Felds mit einem einzelnen Wert

Um die Standardabweichung eines Felds mit nur einem eindeutigen Wert zu berechnen, ist die Standardabweichung 0. Diese Abfrage gruppiert Dokumente, die dem "Vierten Unternehmen" entsprechen. Jedes Geschäft enthält ein einzelnes Dokument und nur einen unterschiedlichen Wert für den Gesamtumsatz.

db.stores.aggregate([{
    $match: {
        company: "Fourth Coffee"
    }
}, {
    $group: {
        _id: "$name",
        stdDev: {
            $stdDevPop: "$sales.totalSales"
        }
    }
}])

Diese Abfrage gibt die folgenden Ergebnisse zurück:

[
  {
      "_id": "Fourth Coffee | Outdoor Equipment Collection - Kochview",
      "stdDev": 0
  },
  {
      "_id": "Fourth Coffee | Grocery Hub - Brakusborough",
      "stdDev": 0
  },
  {
      "_id": "Fourth Coffee | Pet Supply Nook - Lake Armanimouth",
      "stdDev": 0
  },
  {
      "_id": "Fourth Coffee | Beauty Product Nook - Emmytown",
      "stdDev": 0
  },
  {
      "_id": "Fourth Coffee | Bed and Bath Closet - Legroston",
      "stdDev": 0
  },
  {
      "_id": "Fourth Coffee | Automotive Part Collection - Cassinport",
      "stdDev": 0
  }
]

Beispiel 3: Berechnen der Standardabweichung für ein Feld bei Verwendung von Fensteroperatoren

Diese Abfrage berechnet die Standardabweichung des Gesamtumsatzes für Filialen, die zum Unternehmen "First Up Consultants" gehören, vom ersten zum aktuellen Dokument im Resultset.

db.stores.aggregate([{
    $match: {
        company: {
            $in: ["First Up Consultants"]
        },
        $and: [{
            lastUpdated: {
                $gt: ISODate("2024-09-01T03:06:24.180Z")
            }
        }, {
            lastUpdated: {
                "$lt": ISODate("2025-09-30T03:55:17.557Z")
            }
        }]
    }
}, {
    $setWindowFields: {
        partitionBy: "$company",
        sortBy: {
            lastUpdated: 1
        },
        output: {
            stdDevPopTotalSales: {
                $stdDevPop: "$sales.totalSales",
                window: {
                    documents: ["unbounded", "current"]
                }
            }
        }
    }
}, {
    $project: {
        company: 1,
        name: 1,
        "sales.totalSales": 1,
        lastUpdated: 1,
        stdDevPopTotalSales: 1
    }
}])

Diese Abfrage gibt das folgende Ergebnis zurück:

[
    {
        "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f",
        "sales": {},
        "company": "First Up Consultants",
        "lastUpdated": {
            "$date": "2025-06-11T10:48:01.291Z"
        },
        "name": "First Up Consultants | Bed and Bath Center - South Amir",
        "stdDevPopTotalSales": null
    }
]