$pow (aggregation)
定義
$pow
Raises a number to the specified exponent and returns the result.
$pow
has the following syntax:{ $pow: [ <number>, <exponent> ] } The
<number>
expression can be any valid 式 as long as it resolves to a number.The
<exponent>
expression can be any valid 式 as long as it resolves to a number.You cannot raise
0
to a negative exponent.
動作
入力型が混在している場合、$pow
は小さい入力型を 2 つのうち大きい方に昇格させます。型が大きいとみなされるのは、より広い範囲の値を表す場合です。数値型の最小値から最大値の順は、integer → long → double → decimal です。
演算がオーバーフローして、大きい方のデータ型で表される範囲を超えない限り、大きい方の入力型によって結果型も決まります。オーバーフローの場合、$pow
は以下の順序に従って結果を昇格させます。
If either argument resolves to a value of null
or refers to a field that is
missing, $pow
returns null
. If either argument resolves to
NaN
, $pow
returns NaN
.
例 | 結果 |
---|---|
|
|
|
|
|
|
|
|
例
Create a collection called quizzes
with the following documents:
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 variance for each quiz:
db.quizzes.aggregate( [ { $project: { variance: { $pow: [ { $stdDevPop: "$scores.score" }, 2 ] } } } ] )
この操作は次の結果を返します。
{ _id : 1, variance : 64.66666666666667 } { _id : 2, variance : 64.66666666666667 }