$stddevpop演算子は、指定した値の標準偏差を計算します。 演算子は、数値の標準偏差のみを計算できます。
構文
{
$stddevpop: {fieldName}
}
パラメーター
| パラメーター | Description |
|---|---|
fieldName |
標準偏差の計算に使用される値を持つフィールド |
例示
stores コレクションのこのサンプル ドキュメントについて考えてみましょう。
{
"_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 - 1 つの値を持つフィールドの標準偏差を計算する
1 つの個別の値のみを持つフィールドの標準偏差を計算するには、標準偏差は 0 です。 このクエリは、"第 4 の会社" に対応するドキュメントをグループにします。 各ストアには、1 つのドキュメントと、売上合計に対して個別の値が 1 つだけ含まれています。
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
}
]