$nearSphere MongoDB运算符能够返回靠近给定点(指定为运算符的参数)的文档,并按递增距离排序。
然而,如何获得实际距离?有没有什么方法可以使用MongoDB功能来实现这一点?或者使用$nearSphere (尽管我已经检查过its documentation,但我没有找到这些信息),或者使用其他机制(可能是聚合框架?)
谢谢!
发布于 2020-05-15 07:10:00
通过聚合,您可以使用$geoNear stage提供球形几何体并返回距离。
文档中的示例:
db.places.aggregate([
{
$geoNear: {
near: { type: "Point", coordinates: [ -73.99279 , 40.719296 ] },
distanceField: "dist.calculated",
maxDistance: 2,
query: { category: "Parks" },
includeLocs: "dist.location",
spherical: true
}
}
])https://stackoverflow.com/questions/61807565
复制相似问题