On this page本页内容
Changed in version 5.0.在版本5.0中更改。
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.$stdDevSamp
。
$stdDevPop
is available in these stages:可在以下阶段使用:
$addFields
$group
$match
stage that includes an $expr
expression$expr
表达式的$match
阶段$project
$replaceRoot
$replaceWith
$set
$setWindowFields
When used in the 在$bucket
, $bucketAuto
, $group
, and $setWindowFields
stages, $stdDevPop
has this syntax:$bucket
、$bucketAuto
、$group
和$setWindowFields
阶段中使用时,$stdDevPop
具有以下语法:
{ $stdDevPop: <expression> }
When used in other supported stages, 在其他受支持的阶段中使用时,$stdDevPop
has one of two syntaxes:$stdDevPop
具有以下两种语法之一:
$stdDevPop
has one specified expression as its operand:将一个指定的表达式作为其操作数:
{ $stdDevPop: <expression> }
$stdDevPop
has a list of specified expressions as its operand:具有指定表达式列表作为其操作数:
{ $stdDevPop: [ <expression1>, <expression2> ... ] }
The argument for $stdDevPop
can be any expression as long as it resolves to an array.$stdDevPop
的参数可以是任何表达式,只要它解析为数组即可。
For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
$stdDevPop
ignores non-numeric values. 忽略非数值。If all operands for a 如果$stdDevPop
are non-numeric, $stdDevPop
returns null
.$stdDevPop
的所有操作数都是非数字的,则$stdDevPop
将返回null
。
If the sample consists of a single numeric value, 如果样本包含单个数值,$stdDevPop
returns 0
.$stdDevPop
将返回0
。
In the 在$group
and $setWindowFields
stages, if the expression resolves to an array, $stdDevPop
treats the operand as a non-numerical value and has no effect on the calculation.$group
和$setWindowFields
阶段中,如果表达式解析为数组,$stdDevPop
会将操作数视为非数值,对计算没有影响。
In the other supported stages:在其他受支持的阶段中:
$stdDevPop
traverses into the array to operate on the numerical elements of the array to return a single value.$stdDevPop
将遍历数组,对数组的数字元素进行操作,以返回单个值。$stdDevPop
does not traverse into the array but instead treats the array as a non-numerical value.$stdDevPop
不会遍历数组,而是将数组视为非数值。Behavior with values in a $setWindowFields
stage window:$setWindowFields
阶段窗口中值的行为:
null
values, and missing fields in a window.null
值和缺少的字段。null
.null
。NaN
value, returns null
.NaN
值,则返回null
。Infinity
values, returns null
.null
。double
value.double
值。$group
Stage$group
阶段中使用A collection named 名为users
contains the following documents:users
的集合包含以下文档:
{ "_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 }
$project
Stage$project
阶段中使用Create an example collection named 使用以下文档创建名为quizzes
with the following documents:quezzes
的示例集合:
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 }
$setWindowFields
Stage$setWindowFields
阶段中使用New in version 5.0.在版本5.0中新增。
Create a 创建包含加利福尼亚州(cakeSales
collection that contains cake sales in the states of California (CA
) and Washington (WA
):CA
)和华盛顿州(WA
)蛋糕销售的cakeSales
集合:
db.cakeSales.insertMany( [ { _id: 0, type: "chocolate", orderDate: new Date("2020-05-18T14:10:30Z"), state: "CA", price: 13, quantity: 120 }, { _id: 1, type: "chocolate", orderDate: new Date("2021-03-20T11:30:05Z"), state: "WA", price: 14, quantity: 140 }, { _id: 2, type: "vanilla", orderDate: new Date("2021-01-11T06:31:15Z"), state: "CA", price: 12, quantity: 145 }, { _id: 3, type: "vanilla", orderDate: new Date("2020-02-08T13:13:23Z"), state: "WA", price: 13, quantity: 104 }, { _id: 4, type: "strawberry", orderDate: new Date("2019-05-18T16:09:01Z"), state: "CA", price: 41, quantity: 162 }, { _id: 5, type: "strawberry", orderDate: new Date("2019-01-08T06:12:03Z"), state: "WA", price: 43, quantity: 134 } ] )
This example uses 本例使用$stdDevPop
in the $setWindowFields
stage to output the population standard deviation of the cake sales quantity
for each state
:$setWindowFields
阶段中的$stdDevPop
输出每个州蛋糕销售数量的总体标准偏差:
db.cakeSales.aggregate( [ { $setWindowFields: { partitionBy: "$state", sortBy: { orderDate: 1 }, output: { stdDevPopQuantityForState: { $stdDevPop: "$quantity", window: { documents: [ "unbounded", "current" ] } } } } } ] )
In the example:在示例中:
partitionBy: "$state"
state
. state
对集合中的文档进行分区。CA
and WA
.CA
和WA
有分区。sortBy: { orderDate: 1 }
orderDate
in ascending order (1
), so the earliest orderDate
is first.按orderDate
以升序(1)对每个分区中的文档进行排序,因此最早的orderDate
是第一个。output
sets the stdDevPopQuantityForState
field to the quantity
population standard deviation value using $stdDevPop
that is run in a documents window.output
使用在文档窗口中运行的$stdDevPop
将stdDevPopQuantityForState
字段设置为quantity
填充标准偏差值。
The window contains documents between an 该窗口包含输出中处于unbounded
lower limit and the current
document in the output. unbounded
下限和current
文档之间的文档。This means 这意味着$stdDevPop
returns the quantity
population standard deviation value for the documents between the beginning of the partition and the current document.$stdDevPop
返回分区开始处和当前文档之间文档的数量填充标准偏差值。
In this example output, the 在此示例输出中,quantity
population standard deviation value for CA
and WA
is shown in the stdDevPopQuantityForState
field:CA
和WA
的quantity
总体标准偏差值显示在stdDevPopQuantityForState
字段中:
{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"), "state" : "CA", "price" : 41, "quantity" : 162, "stdDevPopQuantityForState" : 0 } { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"), "state" : "CA", "price" : 13, "quantity" : 120, "stdDevPopQuantityForState" : 21 } { "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"), "state" : "CA", "price" : 12, "quantity" : 145, "stdDevPopQuantityForState" : 17.249798710580816 } { "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"), "state" : "WA", "price" : 43, "quantity" : 134, "stdDevPopQuantityForState" : 0 } { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"), "state" : "WA", "price" : 13, "quantity" : 104, "stdDevPopQuantityForState" : 15 } { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"), "state" : "WA", "price" : 14, "quantity" : 140, "stdDevPopQuantityForState" : 15.748015748023622 }