$stdDevPop (aggregation)
Definition
New in version 3.2.
Calculates the population standard deviation of the input values.Use if the values encompass the entire population of data you wantto represent and do not wish to generalize about a largerpopulation. $stdDevPop
ignores non-numeric values.
If the values represent only a sample of a population of data fromwhich to generalize about the population, use $stdDevSamp
instead.
$stdDevPop
is available in the in the following stages:
$group
$project
$addFields
(Available starting in MongoDB 3.4)$set
(Available starting in MongoDB 4.2)$replaceRoot
(Available starting in MongoDB 3.4)$replaceWith
(Available starting in MongoDB 4.2)$match
stage that includes an$expr
expressionWhen used in the$group
stage,$stdDevPop
returnsthe population standard deviation of the specified expression for agroup of documents that share the same group by key and has thefollowing syntax:$stdDevPop
has one specified expression as itsoperand:
- { $stdDevPop: <expression> }
When used in the other supported stages,$stdDevPop
returns the standard deviation of thespecified expression or list of expressions for each document andhas one of two syntaxes:
$stdDevPop
has one specified expression as its operand:
- { $stdDevPop: <expression> }
$stdDevPop
has a list of specified expressions as itsoperand:
- { $stdDevPop: [ <expression1>, <expression2> ... ] }
The argument for $stdDevPop
can be anyexpression as long as it resolvesto an array. For more information on expressions, seeExpressions
Behavior
Non-numeric Values
$stdDevPop
ignores non-numeric values. If all operands for a$stdDevPop
are non-numeric, $stdDevPop
returnsnull
.
Single Value
If the sample consists of a single numeric value, $stdDevPop
returns 0
.
Array Operand
In the $group
stage, if the expression resolves to anarray, $stdDevPop
treats the operand as a non-numerical value.
In the other supported stages:
- With a single expression as its operand, if the expression resolvesto an array,
$stdDevPop
traverses into the array to operate on thenumerical elements of the array to return a single value. - With a list of expressions as its operand, if any of the expressionsresolves to an array,
$stdDevPop
does not traverse into thearray but instead treats the array as a non-numerical value.
Examples
Use in $group Stage
A collection named users
contains the following documents:
- { "_id" : 1, "name" : "dave123", "quiz" : 1, "score" : 85 }
- { "_id" : 2, "name" : "dave2", "quiz" : 1, "score" : 90 }
- { "_id" : 3, "name" : "ahn", "quiz" : 1, "score" : 71 }
- { "_id" : 4, "name" : "li", "quiz" : 2, "score" : 96 }
- { "_id" : 5, "name" : "annT", "quiz" : 2, "score" : 77 }
- { "_id" : 6, "name" : "ty", "quiz" : 2, "score" : 82 }
The following example calculates the standard deviation of each quiz:
- db.users.aggregate([
- { $group: { _id: "$quiz", stdDev: { $stdDevPop: "$score" } } }
- ])
The operation returns the following results:
- { "_id" : 2, "stdDev" : 8.04155872120988 }
- { "_id" : 1, "stdDev" : 8.04155872120988 }
Use in $project Stage
Create an example collection named quizzes
with the followingdocuments:
- db.quizzes.insertMany([
- {
- "_id" : 1,
- "scores" : [
- { "name" : "dave123", "score" : 85 },
- { "name" : "dave2", "score" : 90 },
- { "name" : "ahn", "score" : 71 }
- ]
- },
- {
- "_id" : 2,
- "scores" : [
- { "name" : "li", "quiz" : 2, "score" : 96 },
- { "name" : "annT", "score" : 77 },
- { "name" : "ty", "score" : 82 }
- ]
- }
- ])
The following example calculates the standard deviation of each quiz:
- db.quizzes.aggregate([
- { $project: { stdDev: { $stdDevPop: "$scores.score" } } }
- ])
The operation returns the following results:
- { "_id" : 1, "stdDev" : 8.04155872120988 }
- { "_id" : 2, "stdDev" : 8.04155872120988 }