Docs Menu

$stdDevPop (aggregation)

$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.

If the values represent only a sample of a population of data from which to generalize about the population, use $stdDevSamp instead.

$stdDevPopは、次のステージで使用できます。

$bucket$bucketAuto$group$setWindowFieldsステージで使用する場合、 $stdDevPopは次の構文をとります。

{ $stdDevPop: <expression> }

サポートされている他のステージで使用する場合、 $stdDevPopは次の 2 つの構文のいずれかをとります。

  • $stdDevPopにはオペランドとして指定された式が 1 つあります。

    { $stdDevPop: <expression> }
  • $stdDevPopにはオペランドとして指定された式のリストがあります。

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

$stdDevPopの引数は、配列に解決される限り任意のにすることができます。

式の詳細については、「式演算子」を参照してください。

$stdDevPop returns the population standard deviation of the input values as a decimal.

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

サンプルが単一の数値で構成されている場合、 $stdDevPop0を返します。

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.

サポートされている他のステージでは、次のようになります。

  • 単一の式をオペランドとして使用し、その式が配列に変換されると、$stdDevPop は配列内を走査して配列の数値要素に対して操作を実行し、単一の値を返します。

  • 式のリストをオペランドとして使用する場合に、式のいずれかが配列に変換されると、 は配列内を 走査せず$stdDevPop 、代わりに配列を数値以外の値として扱います。

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

The following example calculates the standard deviation of each quiz:

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

この操作は次の結果を返します。

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

次のドキュメントを使用して、 quizzesという名前のサンプル コレクションを作成します。

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

The following example calculates the standard deviation of each quiz:

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

この操作は次の結果を返します。

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

バージョン 5.0 で追加

カリフォルニア州(CA)とワシントン州(WA)のケーキ販売を含む cakeSales コレクションを作成します。

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 ドキュメント window.

    The ウィンドウ 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.

In this example output, the quantity population standard deviation value for CA and WA is shown in the stdDevPopQuantityForState field:

{ _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 }