$stdDevSamp
New from version 8.0.1.
The $stdDevSamp operator in Amazon DocumentDB calculates the sample standard deviation of numeric values. As an accumulator, it computes the sample standard deviation across documents within a group in the $group stage of an aggregation pipeline. As an expression, it calculates the sample standard deviation of an array of numbers. The sample standard deviation uses N-1 as the divisor (Bessel's correction). Non-numeric values are ignored. If there are fewer than two numeric values, it returns null.
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 $stdDevSamp operator to calculate the sample standard deviation of scores per subject.
Create sample documents
db.scores.insertMany([ { subject: "math", score: 80 }, { subject: "math", score: 90 }, { subject: "math", score: 85 }, { subject: "math", score: 95 }, { subject: "science", score: 70 }, { subject: "science", score: 75 }, { subject: "science", score: 80 }, { subject: "science", score: 85 } ]);
Query example
db.scores.aggregate([ { $group: { _id: "$subject", stdDev: { $stdDevSamp: "$score" } }} ]);
Output
[
{ "_id": "math", "stdDev": 6.454972243679028 },
{ "_id": "science", "stdDev": 6.454972243679028 }
]
Expression usage example (MongoDB Shell)
The $stdDevSamp operator can also be used as an expression within a $project stage to compute the sample 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: { $stdDevSamp: "$measurements" } }} ]);
Output
[
{ "_id": 1, "stdDev": 3.1622776601683795 },
{ "_id": 2, "stdDev": 0 },
{ "_id": 3, "stdDev": 3.1622776601683795 }
]
Code examples
To view a code example for using the $stdDevSamp 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):