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_b
和hypotenuse
存储为128-bit decimal
所以$acos
的输出也是128-bit decimal
。