此版本的文档已存档,不再提供支持。要升级5.0部署,请参阅 MongoDB 6.0升级程序。
定义
行为
null、NaN 和+/- Infinity
如果参数解析为 null 值或引用了缺失的字段,则 $cos 返回 null。如果参数解析为 NaN,则 $cos 返回 NaN。如果参数解析为负无穷大或正无穷大,$cos 将抛出错误。
例子 | 结果 | |||
|---|---|---|---|---|
|
| |||
|
| |||
or
| 抛出一条类似以下格式化输出的错误消息: |
例子
trigonometry 集合包含一个文档,该文档将斜边和一个角存储在直角三角形中:
{ "_id" : ObjectId("5c50782193f833234ba90d85"), "angle_a" : NumberDecimal("53.13010235415597870314438744090659"), "hypotenuse" : NumberDecimal("5") }
以下聚合操作使用$cos表达式计算与angle_a相邻的一侧,并使用$addFields管道阶段将其添加到输入文档。
db.trigonometry.aggregate([ { $addFields : { "side_a" : { $multiply : [ { $cos : {$degreesToRadians : "$angle_a"} }, "$hypotenuse" ] } } } ])
$degreesToRadians 表达式将 angle_a 的度值转换为弧度的等效值。
该命令返回以下输出:
{ "_id" : ObjectId("5c50782193f833234ba90d85"), "angle_a" : NumberDecimal("53.13010235415597870314438744090659"), "side_a" : NumberDecimal("2.999999999999999999999999999999999"), "hypotenuse" : NumberDecimal("5"), }
trigonometry 集合包含一个文档,该文档将斜边和一个角存储在直角三角形中:
{ "_id" : ObjectId("5c50782193f833234ba90d85"), "angle_a" : NumberDecimal("0.9272952180016122324285124629224288"), "hypotenuse" : NumberDecimal("5") }
以下聚合操作使用$cos表达式计算与angle_a相邻的一侧,并使用$addFields管道阶段将其添加到输入文档。
db.trigonometry.aggregate([ { $addFields : { "side_b" : { $multiply : [ { $cos : "$angle_a" }, "$hypotenuse" ] } } } ])
该命令返回以下输出:
{ "_id" : ObjectId("5c50782193f833234ba90d85"), "angle_a" : NumberDecimal("0.9272952180016122324285124629224288"), "side_b" : NumberDecimal("3.000000000000000000000000000000000"), "hypotenuse" : NumberDecimal("5"), }