$stdDevSamp (aggregation)

On this page本页内容

Definition定义

$stdDevSamp

New in version 3.2.版本3.2中的新功能。

Calculates the sample standard deviation of the input values. 计算输入值的样本标准偏差。Use if the values encompass a sample of a population of data from which to generalize about the population. 如果值包含总体数据的样本,则使用该值,从中可以概括总体。$stdDevSamp ignores non-numeric values.忽略非数值。

If the values represent the entire population of data or you do not wish to generalize about a larger population, use $stdDevPop instead.如果这些值代表整个数据总体,或者您不希望概括更大的总体,请使用$stdDevPop

$stdDevSamp 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, $stdDevSamp has the following syntax and returns the sample standard deviation of the specified expression for a group of documents that share the same group by key:$group阶段中使用时,$stdDevSamp具有以下语法,并返回按键共享同一组文档的指定表达式的标准偏差示例:

{ $stdDevSamp: <expression> }

When used in the other supported stages, $stdDevSamp returns the sample standard deviation of the specified expression or list of expressions for each document and has one of two syntaxes:

  • $stdDevSamp has one specified expression as its operand:

    { $stdDevSamp: <expression> }
  • $stdDevSamp has a list of specified expressions as its operand:

    { $stdDevSamp: [ <expression1>, <expression2> ... ]  }

The argument for $stdDevSamp can be any expression as long as it resolves to an array. For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式

Behavior行为

Non-numeric Values

$stdDevSamp ignores non-numeric values. If all operands for a sum are non-numeric, $stdDevSamp returns null.

Single Value

If the sample consists of a single numeric value, $stdDevSamp returns null.

Array Operand

In the $group stage, if the expression resolves to an array, $stdDevSamp 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, $stdDevSamp 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, $stdDevSamp does not traverse into the array but instead treats the array as a non-numerical value.

Example示例

A collection users contains documents with the following fields:

{_id: 0, username: "user0", age: 20}
{_id: 1, username: "user1", age: 42}
{_id: 2, username: "user2", age: 28}
...

To calculate the standard deviation of a sample of users, following aggregation operation first uses the $sample pipeline to sample 100 users, and then uses $stdDevSamp calculates the standard deviation for the sampled users.要计算用户样本的标准偏差,以下聚合操作首先使用$sample管道对100个用户进行采样,然后使用$stdDevSamp计算采样用户的标准偏差。

db.users.aggregate(
   [
      { $sample: { size: 100 } },
      { $group: { _id: null, ageStdDev: { $stdDevSamp: "$age" } } }
   ]
)

The operation returns a result like the following:该操作返回如下结果:

{ "_id" : null, "ageStdDev" : 7.811258386185771 }