$stdDevPop (aggregation)

On this page本页内容

Definition定义

$stdDevPop

New in version 3.2.版本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.$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 the in the following stages:可在以下阶段在中获得:

  • $group
  • $project
  • $addFields (Available starting in MongoDB 3.4)(从MongoDB 3.4开始提供)
  • $set (Available starting in MongoDB 4.2)(从MongoDB 4.2开始提供)
  • $replaceRoot (Available starting in MongoDB 3.4)(从MongoDB 3.4开始提供)
  • $replaceWith (Available starting in MongoDB 4.2)(从MongoDB 4.2开始提供)
  • $match stage that includes an $expr expression包含一个$expr表达式的$match阶段

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:$group阶段中使用时,$stdDevPop返回一组文档的指定表达式的总体标准偏差,这些文档按键共享同一组,并且具有以下语法:语法如下所示:

  • $stdDevPop has one specified expression as its operand:具有一个指定的表达式作为其操作数:

    { $stdDevPop: <expression> }

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返回每个文档的指定表达式或表达式列表的标准偏差,并具有以下两种语法之一:

  • $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行为

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.如果样本由单个数值组成,$stdDevPop返回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:

{ "_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 following documents:

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 }