欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

MongoDB中的$acos聚合函数示例解析

最编程 2024-07-29 17:15:04
...

反余弦度数

trigonometry集合包含下满的文档,存储了直角三角形的3条边长:

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

下面的聚合操作使用$acos表达式计算side_a相邻的角度,并将结果使用$addFields阶段将结果添加到输入文档。

db.trigonometry.aggregate([
  {
    $addFields : {
      "angle_a" : {
        $radiansToDegrees : {
          $acos : {
            $divide : [ "$side_b", "$hypotenuse" ]
          }
        }
      }
    }
  }
])

$radiansToDegrees表达式将$acos返回的弧度转换为角度。

聚合后返回的结果如下:

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

因为side_bhypotenuse存储为128-bit decimal所以$acos的输出也是128-bit decimal