Definition定义
$stdDevSamp
Changed in version 5.0.在版本5.0中的更改。
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.$stdDevSamp忽略非数字值。
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 these stages:在以下阶段可用:
$addFields$group包含$matchstage that includes an$exprexpression$expr表达式的$match阶段$project$replaceRoot$replaceWith$set$setWindowFields(Available starting in MongoDB 5.0)(从MongoDB 5.0开始可用)
Syntax语法
When used in the 在$bucket, $bucketAuto, $group, and $setWindowFields stages, $stdDevSamp has this syntax:$bucket、$bucketAuto、$group和$setWindowFields阶段中使用时,$stdDevSamp具有以下语法:
{ $stdDevSamp: <expression> }
When used in other supported stages, 在其他支持的阶段中使用时,$stdDevSamp has one of two syntaxes:$stdDevSamp有两种语法之一:
$stdDevSamphas one specified expression as its operand:{ $stdDevSamp: <expression> }$stdDevSamphas 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.$stdDevSamp的参数可以是任何表达式,只要它解析为数组即可。
For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
Behavior行为
Result Type结果类型
$stdDevSamp returns the sample standard deviation of the input values as a 将输入值的样本标准偏差返回为double.double。
Non-numeric Values非数值
$stdDevSamp ignores non-numeric values. If all operands for a sum are non-numeric, $stdDevSamp returns null.$stdDevSamp忽略非数字值。如果求和的所有操作数都是非数字的,$stdDevSamp将返回null。
Single Value单一值
If the sample consists of a single numeric value, 如果样本由单个数值组成,$stdDevSamp returns null.$stdDevSamp将返回null。
Array Operand数组运算数
In the 在$group and $setWindowFields stages, if the expression resolves to an array, $stdDevSamp treats the operand as a non-numerical value.$group和$setWindowFields阶段,如果表达式解析为数组,$stdDevSamp会将运算数视为非数值。
In the other supported stages:在其他支持的阶段:
With a single expression as its operand, if the expression resolves to an array,以单个表达式作为操作数,如果表达式解析为数组,$stdDevSamptraverses into the array to operate on the numeric elements of the array to return a single value.$stdDevSamp将遍历数组以对数组的数字元素进行操作,从而返回单个值。With a list of expressions as its operand, if any of the expressions resolves to an array,使用表达式列表作为操作数,如果任何表达式解析为数组,$stdDevSampdoes not traverse into the array but instead treats the array as a non-numeric value.$stdDevSamp不会遍历到数组中,而是将数组视为非数值。
Window Values窗口值
Behavior with values in a 在$setWindowFields stage window:$setWindowFields阶段窗口中使用值的行为:
Ignores non-numeric values,忽略窗口中的非数值、nullvalues, and missing fields in a window.null值和缺失字段。If the window is empty, returns如果窗口为空,则返回null.null。If the window contains a如果窗口包含NaNvalue, returnsnull.NaN值,则返回null。If the window contains如果窗口包含Infinityvalues, returnsnull.Infinity值,则返回null。If none of the previous points apply, returns a如果前面的点都不适用,则返回一个doublevalue.double值。
Examples示例
Use in $group Stage在$group阶段中使用
$group StageA collection 集合users contains documents with the following fields:users包含具有以下字段的文档:
db.users.insertMany( [
{ _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 }Use in $setWindowFields Stage在$setWindowFields阶段中使用
$setWindowFields StageNew in version 5.0.在版本5.0中新增。
Create a 创建一个cakeSales collection that contains cake sales in the states of California (CA) and Washington (WA):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 此示例在$stdDevSamp in the $setWindowFields stage to output the sample standard deviation of the quantity values of the cake sales for each state:$setWindowFields阶段使用$stdDevSamp输出每个state蛋糕销售quantity值的样本标准偏差:
db.cakeSales.aggregate( [
{
$setWindowFields: {
partitionBy: "$state",
sortBy: { orderDate: 1 },
output: {
stdDevSampQuantityForState: {
$stdDevSamp: "$quantity",
window: {
documents: [ "unbounded", "current" ]
}
}
}
}
}
] )
In the example:在示例中:
partitionBy: "$state"partitions the documents in the collection by按state. There are partitions forCAandWA.state对集合中的文档进行分区。CA和WA有分区。sortBy: { orderDate: 1 }sorts the documents in each partition by按orderDatein ascending order (1), so the earliestorderDateis first.orderDate升序(1)对每个分区中的文档进行排序,因此最早的orderDate位居前列。
outputsets the使用在stdDevSampQuantityForStatefield to the sample standard deviation of thequantityvalues using$stdDevSampthat is run in a documents window.documents窗口中运行的$stdDevSamp将stdDevSampQuantityForState字段设置为quantity值的样本标准偏差。The window contains documents between an该窗口包含的文档位于unboundedlower limit and thecurrentdocument in the output.unbounded下限和输出中的current文档之间。This means这意味着$stdDevSampreturns the sample standard deviation of thequantityvalues for the documents between the beginning of the partition and the current document.$stdDevSamp返回分区开始和当前文档之间文档quantity值的样本标准偏差。
In this output, the sample standard deviation 在此输出中,quantity value for CA and WA is shown in the stdDevSampQuantityForState field:CA和WA的样本标准偏差quantity值显示在stdDevSampQuantityForState字段中:
{ _id: 4, type: "strawberry", orderDate: ISODate("2019-05-18T16:09:01Z"),
state: "CA", price: 41, quantity: 162, stdDevSampQuantityForState: null }
{ _id: 0, type: "chocolate", orderDate: ISODate("2020-05-18T14:10:30Z"),
state: "CA", price: 13, quantity: 120, stdDevSampQuantityForState: 29.698484809834994 }
{ _id: 2, type: "vanilla", orderDate: ISODate("2021-01-11T06:31:15Z"),
state: "CA", price: 12, quantity: 145, stdDevSampQuantityForState: 21.1266025033211 }
{ _id: 5, type: "strawberry", orderDate: ISODate("2019-01-08T06:12:03Z"),
state: "WA", price: 43, quantity: 134, stdDevSampQuantityForState: null }
{ _id: 3, type: "vanilla", orderDate: ISODate("2020-02-08T13:13:23Z"),
state: "WA", price: 13, quantity: 104, stdDevSampQuantityForState: 21.213203435596427 }
{ _id: 1, type: "chocolate", orderDate: ISODate("2021-03-20T11:30:05Z"),
state: "WA", price: 14, quantity: 140, stdDevSampQuantityForState: 19.28730152198591 }