$stdDevPop
New from version 8.0.1.
The $stdDevPop operator in Amazon DocumentDB calculates the population standard deviation of numeric values. As an accumulator, it computes the population standard deviation across documents within a group in the $group stage of an aggregation pipeline. As an expression, it calculates the population standard deviation of an array of numbers. The population standard deviation uses N as the divisor (not N-1). Non-numeric values are ignored. If there are no numeric values, it returns null. If there is only one numeric value, it returns 0.
Parameters
-
expression: An expression that resolves to a numeric value or an array of numeric values.
Example (MongoDB Shell)
The following example demonstrates the use of the $stdDevPop operator to calculate the population standard deviation of scores per subject.
Create sample documents
db.scores.insertMany([ { subject: "math", score: 60 }, { subject: "math", score: 75 }, { subject: "math", score: 85 }, { subject: "math", score: 92 }, { subject: "math", score: 78 }, { subject: "science", score: 55 }, { subject: "science", score: 70 }, { subject: "science", score: 82 }, { subject: "science", score: 91 }, { subject: "science", score: 67 } ]);
Query example
db.scores.aggregate([ { $group: { _id: "$subject", stdDev: { $stdDevPop: "$score" } }} ]);
Output
[
{ "_id": "math", "stdDev": 10.75174404457249 },
{ "_id": "science", "stdDev": 12.441864811996632 }
]
Expression usage example (MongoDB Shell)
The $stdDevPop operator can also be used as an expression within a $project stage to compute the population standard deviation of an array field.
Create sample documents
db.experiments.insertMany([ { _id: 1, measurements: [10, 12, 14, 16, 18] }, { _id: 2, measurements: [5, 5, 5, 5, 5] }, { _id: 3, measurements: [2, 4, 6, 8, 10] } ]);
Query example
db.experiments.aggregate([ { $project: { stdDev: { $stdDevPop: "$measurements" } }} ]);
Output
[
{ "_id": 1, "stdDev": 2.8284271247461903 },
{ "_id": 2, "stdDev": 0 },
{ "_id": 3, "stdDev": 2.8284271247461903 }
]
Code examples
To view a code example for using the $stdDevPop operator, choose the tab for the language that you want to use. The following examples show both accumulator usage (in $group) and expression usage (in $project):