Database Manual / Reference / Query Language / Accumulators

$stdDevPop (accumulator operator)(累加器运算符)

Definition定义

$stdDevPop

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.$stdDevPop忽略非数字值。

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:在以下阶段可用:

Syntax语法

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.有关表达式的详细信息,请参阅表达式

Behavior行为

Result Type

$stdDevPop returns the population standard deviation of the input values as a decimal.decimal形式返回输入值的总体标准偏差。

Non-numeric Values非数值

$stdDevPop ignores non-numeric values. If all operands for a $stdDevPop are non-numeric, $stdDevPop returns null.$stdDevPop忽略非数字值。如果$stdDevPop的所有操作数都是非数字的,则$stdDevPop将返回null

Single Value单一值

If the sample consists of a single numeric value, $stdDevPop returns 0.如果样本由单个数值组成,$stdDevPop将返回0

Array Operand数组操作对象

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:在其他支持的阶段:

  • With a single expression as its operand, if the expression resolves to an array, $stdDevPop traverses into the array to operate on the numeric elements of the array to return a single value.以单个表达式作为操作数,如果表达式解析为数组,$stdDevPop将遍历数组以对数组的数字元素进行操作,从而返回单个值。
  • 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-numeric value.使用表达式列表作为操作数,如果任何表达式解析为数组,$stdDevPop不会遍历到数组中,而是将数组视为非数值。

Window Values窗口值

Behavior with values in a $setWindowFields stage window:$setWindowFields阶段窗口中使用值的行为:

  • Ignores non-numeric values, null values, and missing fields in a window.忽略窗口中的非数值、null值和缺失字段。
  • If the window is empty, returns null.如果窗口为空,则返回null
  • If the window contains a NaN value, returns null.如果窗口包含NaN值,则返回null
  • If the window contains Infinity values, returns null.如果窗口包含Infinity值,则返回null
  • If none of the previous points apply, returns a double value.如果前面的点都不适用,则返回一个double值。

Examples示例

Use in $group Stage$group阶段中使用

Create a collection called users with the following documents:使用以下文档创建一个名为users的集合:

db.users.insertMany( [
{ _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$project阶段使用

Create an example collection named quizzes with the following documents:使用以下文档创建一个名为quizzes的示例集合:

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 }

Use in $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):创建一个cakeSales集合,其中包含加利福尼亚州(CA)和华盛顿州(WA)的蛋糕销售:

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输出每个state的蛋糕销售quantity的总体标准偏差:

db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { orderDate: 1 },
output: {
stdDevPopQuantityForState: {
$stdDevPop: "$quantity",
window: {
documents: [ "unbounded", "current" ]
}
}
}
}
}
] )

In the example:在示例中:

  • partitionBy: "$state" partitions the documents in the collection by state. There are partitions for CA and WA.state对集合中的文档进行分区CAWA有分区。
  • sortBy: { orderDate: 1 } sorts the documents in each partition by 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.使用在documents窗口中运行的$stdDevPopstdDevPopQuantityForState字段设置为quantity的总体标准偏差值。

    The window contains documents between an unbounded lower limit and the current document in the output. This means $stdDevPop returns the quantity population standard deviation value for the documents between the beginning of the partition and the current document.窗口包含的文档位于unbounded下限和输出中的current文档之间。这意味着$stdDevPop返回分区开始和当前文档之间的文档quantity的总体标准偏差值。

In this example output, the quantity population standard deviation value for CA and WA is shown in the stdDevPopQuantityForState field:在此示例输出中,CAWAquantity的总体标准偏差值显示在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 }