对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
See how MongoDB 9.0 delivers up to 2x higher throughput.
MongoDB Branding Shape
Register now >
Docs 菜单

$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"
}
}
}
] )

在前面的管道中,带有 $stdDevSamp 的 $project 计算每个学生的 scores大量的示例标准差并将其作为 scoreStdDev 返回。

给本页内容打分