$stddevpop

$stddevpop 运算符计算指定值的标准偏差。 运算符只能计算数值的标准偏差。

Syntax

{
  $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 - 使用窗口运算符时计算字段的标准偏差

此查询计算属于“第一向上顾问”公司的商店总销售额的标准偏差,从结果集中的第一个到当前文档。

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