- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $stdDevPop (aggregation)
$stdDevPop (aggregation)¶
On this page
Definition¶
-
$stdDevPop
¶ 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 want to represent and do not wish to generalize about a larger population.
$stdDevPop
ignores non-numeric values.If the values represent only a sample of a population of data from which 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
expression
When used in the
$group
stage,$stdDevPop
returns the population standard deviation of the specified expression for a group of documents that share the same group by key and has the following syntax:$stdDevPop
has one specified expression as its operand:
When used in the other supported stages,
$stdDevPop
returns the standard deviation of the specified expression or list of expressions for each document and has one of two syntaxes:$stdDevPop
has one specified expression as its operand:$stdDevPop
has a list of specified expressions as its operand:
The argument for
$stdDevPop
can be any expression as long as it resolves to an array. For more information on expressions, see Expressions
Behavior¶
Non-numeric Values¶
$stdDevPop
ignores non-numeric values. If all operands for a
$stdDevPop
are non-numeric, $stdDevPop
returns
null
.
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 an
array, $stdDevPop
treats the operand as a non-numerical value.
In the other supported stages:
- With a single expression as its operand, if the expression resolves
to an array,
$stdDevPop
traverses into the array to operate on the numerical elements of the array to return a single value. - With a list of expressions as its operand, if any of the expressions
resolves to an array,
$stdDevPop
does not traverse into the array but instead treats the array as a non-numerical value.
Examples¶
Use in $group
Stage¶
A collection named users
contains the following documents:
The following example calculates the standard deviation of each quiz:
The operation returns the following results:
Use in $project
Stage¶
Create an example collection named quizzes
with the following
documents:
The following example calculates the standard deviation of each quiz:
The operation returns the following results: