Docs Menu

$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 は以下の順序に従って結果を昇格させます。

  • より大きな入力型が integer の場合、結果型は long に昇格します。

  • より大きな入力型が long の場合、結果型は double に昇格します。

  • 大きい方の型が double または decimal の場合、オーバーフロー結果は正または負の無限大として表されます。結果型の昇格は行われません。

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.

結果

{ $pow: [ 5, 0 ] }

1

{ $pow: [ 5, 2 ] }

25

{ $pow: [ 5, -2 ] }

0.04

{ $pow: [ -5, 0.5 ] }

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 }

項目一覧