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

$stdDevPop(累加器操作符)

$stdDevPop

在版本5.0中进行了更改。

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.

如果这些值只是用来推断全体数据特征的样本数据,请改用 $stdDevSamp

$stdDevPop 可在以下阶段使用:

When used in the $bucket, $bucketAuto, $group, and $setWindowFields stages, $stdDevPop has this syntax:

{ $stdDevPop: <expression> }

When used in other supported stages, $stdDevPop has one of two syntaxes:

  • $stdDevPop 有一个指定的表达式作为其操作数:

    { $stdDevPop: <expression> }
  • $stdDevPop 有一个指定表达式组成的列表作为其操作数:

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

The argument for $stdDevPop can be any expression as long as it resolves to an array.

有关表达式的更多信息,请参阅表达式

$stdDevPopdouble形式返回输入值的总体标准差。

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

If the sample consists of a single numeric value, $stdDevPop returns 0.

In the $group and $setWindowFields stages, if the expression resolves to an array, $stdDevPop treats the operand as a non-numerical value and has no effect on the calculation.

在其他支持的阶段:

  • With a single expression as its operand, if the expression resolves to an array, $stdDevPop traverses into the array to operate on the numeric 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-numeric value.

$setWindowFields阶段窗口中具有值的行为:

  • 忽略窗口中的非数字值、 null值和缺失字段。

  • 如果窗口为空,则返回null

  • 如果窗口包含NaN值,则返回null

  • 如果窗口包含Infinity值,则返回null

  • 如果前面的点都不适用,则返回double值。

使用以下文档创建名为 users 的集合:

db.users.insertMany( [
{ _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 }
] )

以下示例计算每次测验的标准差:

db.users.aggregate( [
{ $group: { _id: "$quiz", stdDev: { $stdDevPop: "$score" } } }
] )

操作返回以下结果:

{ "_id" : 2, "stdDev" : 8.04155872120988 }
{ "_id" : 1, "stdDev" : 8.04155872120988 }

使用以下文档创建名为quizzes的示例collection:

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

以下示例计算每次测验的标准差:

db.quizzes.aggregate( [
{ $project: { stdDev: { $stdDevPop: "$scores.score" } } }
] )

操作返回以下结果:

{ _id : 1, stdDev : 8.04155872120988 }
{ _id : 2, stdDev : 8.04155872120988 }

版本 5.0 中的新增功能。

创建cakeSales集合,其中包含加利福尼亚州 ( CA ) 和华盛顿州 ( WA ) 的蛋糕销售情况:

db.cakeSales.insertMany( [
{ _id: 0, type: "chocolate", orderDate: new Date("2020-05-18T14:10:30Z"),
state: "CA", price: 13, quantity: 120 },
{ _id: 1, type: "chocolate", orderDate: new Date("2021-03-20T11:30:05Z"),
state: "WA", price: 14, quantity: 140 },
{ _id: 2, type: "vanilla", orderDate: new Date("2021-01-11T06:31:15Z"),
state: "CA", price: 12, quantity: 145 },
{ _id: 3, type: "vanilla", orderDate: new Date("2020-02-08T13:13:23Z"),
state: "WA", price: 13, quantity: 104 },
{ _id: 4, type: "strawberry", orderDate: new Date("2019-05-18T16:09:01Z"),
state: "CA", price: 41, quantity: 162 },
{ _id: 5, type: "strawberry", orderDate: new Date("2019-01-08T06:12:03Z"),
state: "WA", price: 43, quantity: 134 }
] )

This example uses $stdDevPop in the $setWindowFields stage to output the population standard deviation of the cake sales quantity for each state:

db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { orderDate: 1 },
output: {
stdDevPopQuantityForState: {
$stdDevPop: "$quantity",
window: {
documents: [ "unbounded", "current" ]
}
}
}
}
}
] )

在示例中:

  • partitionBy: "$state"state 对集合中的文档分区CAWA 都有分区。

  • sortBy: { orderDate: 1 }orderDate 以升序 (1) 对每个分区中的文档进行排序,因此最早的 orderDate 位于最前面。

  • output sets the stdDevPopQuantityForState field to the quantity population standard deviation value using $stdDevPop that is run in a documents window.

    The window contains documents between an unbounded lower limit and the current document in the output. This means $stdDevPop returns the quantity population standard deviation value for the documents between the beginning of the partition and the current document.

在此示例输出中,CAWAquantity 总体标准差值显示在 stdDevPopQuantityForState 字段中:

{ _id : 4, type : "strawberry", orderDate : ISODate("2019-05-18T16:09:01Z"),
state : "CA", price : 41, quantity : 162, stdDevPopQuantityForState : 0 }
{ _id : 0, type : "chocolate", orderDate : ISODate("2020-05-18T14:10:30Z"),
state : "CA", price : 13, quantity : 120, stdDevPopQuantityForState : 21 }
{ _id : 2, type : "vanilla", orderDate : ISODate("2021-01-11T06:31:15Z"),
state : "CA", price : 12, quantity : 145, stdDevPopQuantityForState : 17.249798710580816 }
{ _id : 5, type : "strawberry", orderDate : ISODate("2019-01-08T06:12:03Z"),
state : "WA", price : 43, quantity : 134, stdDevPopQuantityForState : 0 }
{ _id : 3, type : "vanilla", orderDate : ISODate("2020-02-08T13:13:23Z"),
state : "WA", price : 13, quantity : 104, stdDevPopQuantityForState : 15 }
{ _id : 1, type : "chocolate", orderDate : ISODate("2021-03-20T11:30:05Z"),
state : "WA", price : 14, quantity : 140, stdDevPopQuantityForState : 15.748015748023622 }
给本页内容打分