$atan2y / xの逆タンジェント(アーク タンジェント)を返します。yとxはそれぞれ式に渡される最初と 2 番目の値です。$atan2の構文は次のとおりです。{ $atan2: [ <expression 1>, <expression 2> ] } $atan2takes any valid expression that resolves to a number.$atan2returns values in radians. Use$radiansToDegreesoperator to convert the output value from radians to degrees.$atan2デフォルトでは、 は値をdoubleとして返します。$atan2128<expression>は、 が128 ビットの 10 進数値に解決される限り、 ビットの 10 進数として値を返すこともできます。式の詳細については、「式 」を参照してください。
動作
null および NaN
$atan2に指定された引数のいずれかがnullの場合、式はnullを返します。 いずれかの引数がNaNの場合、式はNaNを返します。 1 つの引数がnullで、もう 1 つの引数がNaNの場合、式はnullを返します。
例 | 結果 |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
例
trigonometryコレクションには、直角三角形の 3 つの面を保存するドキュメントが含まれています。
{ "_id" : ObjectId("5c50782193f833234ba90d85"), "side_a" : Decimal128("3"), "side_b" : Decimal128("4"), "hypotenuse" : Decimal128("5") }
The following aggregation operation uses the $atan2 expression to calculate the angle adjacent to side_a and add it to the input document using the $addFields pipeline stage.
db.trigonometry.aggregate([ { $addFields : { "angle_a" : { $radiansToDegrees : { $atan2 : [ "$side_b", "$side_a" ] } } } } ])
The $radiansToDegrees expression converts the radian value returned by $atan2 to the equivalent value in degrees.
このコマンドは、次の出力を返します。
{ "_id" : ObjectId("5c50782193f833234ba90d85"), "side_a" : Decimal128("3"), "side_b" : Decimal128("4"), "hypotenuse" : Decimal128("5"), "angle_a" : Decimal128("53.13010235415597870314438744090658") }
side_bと はside_a 128ビットの 10 進数として保存されているため、$atan2 の出力は128 ビットの 10 進数になります。
trigonometryコレクションには、直角三角形の 3 つの面を保存するドキュメントが含まれています。
{ "_id" : ObjectId("5c50782193f833234ba90d85"), "side_a" : Decimal128("3"), "side_b" : Decimal128("4"), "hypotenuse" : Decimal128("5") }
The following aggregation operation uses the $atan2 expression to calculate the angle adjacent to side_a and add it to the input document using the $addFields pipeline stage.
db.trigonometry.aggregate([ { $addFields : { "angle_a" : { $atan2 : [ "$side_b", "$side_a" ] } } } ])
このコマンドは、次の出力を返します。
{ "_id" : ObjectId("5c50782193f833234ba90d85"), "side_a" : Decimal128("3"), "side_b" : Decimal128("4"), "hypotenuse" : Decimal128("5"), "angle_a" : Decimal128("0.9272952180016122324285124629224287") }
Since side_b and side_a are stored as 128-bit decimals, the output of $atan2 is a 128-bit decimal.