对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。

$stdDevSamp (表达式)

$stdDevSamp

返回数组中数值的样本标准偏差。

注意

消歧

此页面介绍 $stdDevSamp 当作为 表达式 使用时,它返回数组中数值的示例标准差。

您还可以在这些其他上下文中使用 $stdDevSamp:

当作为表达式使用时,$stdDevSamp 具有以下语法之一:

{ $stdDevSamp: <expression> }

在上述语法中,操作数是一个单一的表达式,该表达式的求值结果为数组。$stdDevSamp 遍历数组并返回数组的数值的样本标准偏差。

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

在前面的语法中,每个表达式应评估为单个数值。$stdDevSamp 返回这些值的样本标准偏差。非数值被忽略。

$stdDevSamp 以double形式返回输入值的示例标准差。

$stdDevSamp 忽略非数字值、空值和缺失字段。如果所有输入值非数值或缺失,$stdDevSamp 返回 null。

如果只有一个数值输入值,$stdDevSamp 返回 null。

创建一个 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 ] }] )

以下管道使用 $stdDevSamp 计算每位学生考试成绩的示例标准差:

db.testScores.aggregate( [ { $project: { _id: 0, studentId: 1, scoreStdDev: { $stdDevSamp: "$scores" } } }] )

在前面的管道中,$project 使用 $stdDevSamp 计算每个学生的 scores 数组的示例标准偏差,并将其作为 scoreStdDev 返回。

给本页内容打分