Docs Menu

$tan (aggregation)

項目一覧

$tan

Returns the tangent of a value that is measured in radians.

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

{ $tan: <expression> }

$tan takes any valid that resolves to a number. If the expression returns a value in degrees, use the $degreesToRadians operator to convert the result to radians.

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

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

If the argument resolves to a value of null or refers to a field that is missing, $tan returns null. If the argument resolves to NaN, $tan returns NaN. If the argument resolves to negative or positive infinity, $tan throws an error.

結果

{ $tan: NaN }

NaN

{ $tan: null }

null

{ $tan : Infinity}

or

{ $tan : -Infinity }

次の形式の出力のようなエラーメッセージをスローします。

"errmsg" :
"Failed to optimize pipeline :: caused by :: cannot
apply $tan to -inf, value must in (-inf,inf)"

The trigonometry collection contains a document that stores one side and one angle in a right-angle triangle:

{
"_id" : ObjectId("5c50782193f833234ba90d85"),
"angle_a" : NumberDecimal("53.13010235415597870314438744090659"),
"side_a" : NumberDecimal("3")
}

The following aggregation operation uses the $tan expression to calculate the side opposite to angle_a and add it to the input document using the $addFields pipeline stage.

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

The $degreesToRadians expression converts the degree value of angle_a to the equivalent value in radians.

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

{
"_id" : ObjectId("5c50782193f833234ba90d85"),
"angle_a" : NumberDecimal("53.13010235415597870314438744090659"),
"side_a" : NumberDecimal("3")
"side_b" : NumberDecimal(4.000000000000000000000000000000000")
}

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

The trigonometry collection contains a document that stores the hypotenuse and one angle in a right-angle triangle:

{
"_id" : ObjectId("5c50782193f833234ba90d85"),
"angle_a" : NumberDecimal("0.9272952180016122324285124629224288"),
"side_a" : NumberDecimal("3")
}

The following aggregation operation uses the $tan expression to calculate the side adjacent to angle_a and add it to the input document using the $addFields pipeline stage.

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

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

{
"_id" : ObjectId("5c50782193f833234ba90d85"),
"angle_a" : NumberDecimal("0.9272952180016122324285124629224288"),
"side_a" : NumberDecimal("3")
"side_b" : NumberDecimal("3.999999999999999999999999999999999")
}

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

項目一覧