Docs Menu

$sin (aggregation)

項目一覧

$sin

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

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

{ $sin: <expression> }

$sin 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.

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

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

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

結果

{ $sin: NaN }

NaN

{ $sin: null }

null

{ $sin : Infinity}

or

{ $sin : -Infinity }

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

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

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

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

The following aggregation operation uses the $sin 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 : [
{ $sin : {$degreesToRadians : "$angle_a"} },
"$hypotenuse"
]
}
}
}
])

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_b" : NumberDecimal("4.000000000000000000000000000000000"),
"hypotenuse" : NumberDecimal("5"),
}

angle_ahypotenuse128ビットの 10 進数 として保存されているため、 $sinの出力は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"),
"hypotenuse" : NumberDecimal("5")
}

The following aggregation operation uses the $sin 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 : [
{ $sin : "$angle_a" },
"$hypotenuse"
]
}
}
}
])

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

{
"_id" : ObjectId("5c50782193f833234ba90d85"),
"angle_a" : NumberDecimal("0.9272952180016122324285124629224288"),
"side_b" : NumberDecimal("4.000000000000000000000000000000000"),
"hypotenuse" : NumberDecimal("5"),
}

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

項目一覧