Docs Menu

$atan 2(集計)

項目一覧

$atan2

Returns the inverse tangent (arc tangent) of y / x, where y and x are the first and second values passed to the expression respectively.

$atan2の構文は次のとおりです。

{ $atan2: [ <expression 1>, <expression 2> ] }

$atan2 takes any valid that resolves to a number.

$atan2はラジアン単位で値を返します。 $radiansToDegrees演算子を使用して出力値をラジアンから度に変換します。

デフォルトでは、 $atan2は値をdoubleとして返します。 $atan2は、 <expression>が128ビットの 10 進数値に解決される限り、 128ビットの 10 進数として値を返すこともできます。

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

If either argument given to $atan2 is null, the expression returns null. If either argument is NaN, the expression returns NaN. If one argument is null and the other is NaN, the expression returns null.

結果

{ $atan2: [ NaN, <value> ] }

NaN

{ $atan2: [ <value>, NaN ] }

NaN

{ $atan2: [ null, <value> ] }

null

{ $atan2: [ <value>, null ] }

null

{ $atan2: [ NaN, null ] }

null

{ $atan2: [ null, NaN ] }

null

trigonometryコレクションには、直角三角形の 3 つの面を保存するドキュメントが含まれています。

{
"_id" : ObjectId("5c50782193f833234ba90d85"),
"side_a" : NumberDecimal("3"),
"side_b" : NumberDecimal("4"),
"hypotenuse" : NumberDecimal("5")
}

次の集計操作では、 $atan2式を使用してside_aに隣接する角度を計算し、それを$addFieldsパイプライン ステージを使用して入力ドキュメントに追加します。

db.trigonometry.aggregate([
{
$addFields : {
"angle_a" : {
$radiansToDegrees : {
$atan2 : [ "$side_b", "$side_a" ]
}
}
}
}
])

$radiansToDegrees式は、 $atan2によって返されたラジアン値を度単位の同等の値に変換します。

このコマンドは、次の出力を返します。

{
"_id" : ObjectId("5c50782193f833234ba90d85"),
"side_a" : NumberDecimal("3"),
"side_b" : NumberDecimal("4"),
"hypotenuse" : NumberDecimal("5"),
"angle_a" : NumberDecimal("53.13010235415597870314438744090658")
}

side_bside_a128ビットの 10 進数 として保存されているため、 $atan2の出力は128ビットの 10 進数になります。

trigonometryコレクションには、直角三角形の 3 つの面を保存するドキュメントが含まれています。

{
"_id" : ObjectId("5c50782193f833234ba90d85"),
"side_a" : NumberDecimal("3"),
"side_b" : NumberDecimal("4"),
"hypotenuse" : NumberDecimal("5")
}

次の集計操作では、 $atan2式を使用してside_aに隣接する角度を計算し、それを$addFieldsパイプライン ステージを使用して入力ドキュメントに追加します。

db.trigonometry.aggregate([
{
$addFields : {
"angle_a" : {
$atan2 : [ "$side_b", "$side_a" ]
}
}
}
])

このコマンドは、次の出力を返します。

{
"_id" : ObjectId("5c50782193f833234ba90d85"),
"side_a" : NumberDecimal("3"),
"side_b" : NumberDecimal("4"),
"hypotenuse" : NumberDecimal("5"),
"angle_a" : NumberDecimal("0.9272952180016122324285124629224287")
}

side_bside_a128ビットの 10 進数 として保存されているため、 $atan2の出力は128ビットの 10 進数になります。

項目一覧