$stddevpop

運算子 $stddevpop 計算指定值的標準差。 運算子只能計算數值的標準差。

語法

{
  $stddevpop: {fieldName}
}

參數

參數 Description
fieldName 其值用於計算標準差的欄位

範例

請參考商店集合中的此範例檔。

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

範例 1 - 計算總銷售額的標準差

若要計算屬於「Fourth Coffee」的商店所有銷售類別的總銷售額的標準差,請先篩選公司欄位,然後使用 stddevpop 計算所有產生商店的總銷售額,並傳回彙總結果。

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

此查詢會傳回下列結果:

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

範例 2 - 計算具有單一值的欄位標準差

若要計算只有一個不同值的欄位的標準差,標準差為 0。 此查詢用於將「第四公司」相關的文件進行分組。 每個商店都包含一個文件,且只有一個不同的總銷售額值。

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

此查詢會傳回下列結果:

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

範例 3 - 使用視窗運算子時計算欄位的標準差

此查詢會計算屬於「First Up Consultants」公司的商店從結果集中第一個文件到目前文件的總銷售額標準差。

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

此查詢會傳回下列結果:

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