定义
返回数组中数值的总体标准偏差。
注意
消歧
本页介绍作为 $stdDevPop 使用时的 表达式。
您还可以在这些其他上下文中使用 $stdDevPop:
$stdDevPop (accumulator), 返回一组文档中值的总体标准偏差。$stdDevPop (window function),用于在$setWindowFields阶段计算特定窗口中文档值的总体标准差。
语法
作为表达式使用时,$stdDevPop 具有以下两种语法之一:
{ $stdDevPop: <expression> }在上述语法中,操作数是一个单一的表达式,该表达式的求值结果为数组。$stdDevPop 遍历数组,并返回数组中数值的总体标准偏差。
{ $stdDevPop: [ <expression1>, <expression2>, ... ] }在前面的语法中,每个表达式应评估为单个数值。$stdDevPop 返回这些值的总体标准偏差。非数值被忽略。
行为
结果类型
$stdDevPop 以double形式返回输入值的总体标准差。
非数值或缺失值
$stdDevPop 忽略非数字值,包括缺失值。如果所有输入值均非数字,则 $stdDevPop 会返回 null。
单值
如果输入由单个数值组成,$stdDevPop 将返回 0。
例子
创建一个 testScores 集合:
db.testScores.insertMany( [ { studentId: "2345", scores: [ 62, 81, 80, 75, 68, 72, 74, 78, 69, 77 ] }, { studentId: "2356", scores: [ 60, 83, 79, 72, 65, 71, 68, 74, 64, 76 ] }, { studentId: "2358", scores: [ 67, 82, 78, 70, 84, 73, 79, 71, 88, 75 ] }, { studentId: "2367", scores: [ 64, 72, 77, 58, 91, 66, 83, 55, 94, 61 ] }, { studentId: "2369", scores: [ 60, 53, 72, 85, 63, 88, 57, 79, 66, 74 ] }] )以下管道使用 $stdDevPop 计算每个学生测试分数的总体标准差:
db.testScores.aggregate( [ { $project: { _id: 0, studentId: 1, scoreStdDev: { $stdDevPop: "$scores" } } }] )在前面的管道中,带有 $stdDevPop 的 $project 计算每个学生的 scores数组的总体标准偏差,并将其作为 scoreStdDev 返回。