$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입니다. 이 쿼리는 "Fourth Company"에 해당하는 문서를 그룹화합니다. 각 저장소에는 단일 문서와 총 판매액에 대한 하나의 고유 값만 포함됩니다.
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
}
]