db.collection('Info')
.aggregate()
.addFields({
Degree: getDistance('$OriginLatitude','$OriginLongitude','$DestinationLatitude','$DestinationLongitude')
})
.end()
.then(res => {
console.log(res)
})
.catch(err => {
})
云函数中,聚合使用addFields时,getDistance是自定义函数,里面用了'$OriginLatitude'这些字段变量,得到的Degree为null,是语法不对吗?把'$OriginLatitude'这些变量写成阿拉伯数字就能正常运行。。。
报错信息如下:Error: errCode: InternalServerError | errMsg: Command failed with error 40177 (Location40177): 'Invalid $addFields :: caused by :: specification must have at least one field' on server 192.168.146.77:3717. The full response is { "operationTime" : { "$timestamp" : { "t" : 1623577982, "i" : 114 } }, "ok" : 0.0, "errmsg" : "Invalid $addFields :: caused by :: specification must have at least one field", "code" : 40177, "codeName" : "Location40177", "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 1623577982, "i" : 114 } }, "signature" : { "hash" : { "$binary" : "b+SJ2ETY9Y1viMlZiPRk84oQMq4=", "$type" : "00" }, "keyId" : { "$numberLong" : "6941880109229932637" } } } }
好难理解,为啥呢这是?难道是bug吗?
Neveregret (作者)
计算Degree是个比较复杂的算式,需要用到'$OriginLatitude'等数据库字段,也需要用到数据库外面传进来的几个值,不使用自定义函数的话,如何进行计算呢?卡在这过不去了,麻烦大佬指点一下,感谢!
2021-06-14 11:17
Neveregret (作者)
求大佬帮忙解决以下,卡在这真的是没办法了。。。
2021-06-15 10:54
DCloud_uniCloud_WYQ
回复 Neveregret: 查询出来再计算
2021-06-16 10:59