对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。

$sin(表达式操作符)

$sin

返回以弧度为单位测量的某一值的正弦值。

$sin 通过以下语法实现:

{ $sin: <expression> }

$sin 接受所有解析为数字的有效表达式。如果表达式返回以度为单位的值,请使用 $degreesToRadians 操作符将结果转换为弧度。

默认情况下,$sin 会以 double 的形式返回值。只要 <expression> 解析为 128 位十进制值, $sin 也会返回 128 位十进制值。

有关表达式的更多信息,请参阅表达式。

如果参数解析为 null 值或引用了缺失的字段,则 $sin 返回 null。如果参数解析为 NaN,则 $sin 返回 NaN。如果参数解析为负无穷大或正无穷大,$sin 将抛出错误。

例子
结果

{ $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)"

trigonometry 集合包含一个文档,该文档将斜边和一个角存储在直角三角形中:

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

下面的聚合操作使用 $sin 表达式计算与 angle_a 相对的边,并使用 $addFields 管道阶段将其添加到输入文档中。

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

$degreesToRadians 表达式将 angle_a 的度值转换为弧度的等效值。

该命令返回以下输出:

{ "_id" : ObjectId("5c50782193f833234ba90d85"), "angle_a" : Decimal128("53.13010235415597870314438744090659"), "side_b" : Decimal128("4.000000000000000000000000000000000"), "hypotenuse" : Decimal128("5"),}

由于 angle_a 和 hypotenuse 存储为 128 位小数,因此$sin 的输出为 128 位小数。

给本页内容打分

在此页面上