$nearSphere
MongoDB 运算符能够返回按递增距离排序的给定点(指定为运算符的参数)附近的文档。
但是,如何获得实际距离?有没有办法使用 MongoDB 功能来获得这个?使用$nearSphere
(尽管我检查了它的文档并且我没有找到此信息)或使用其他机制(可能是聚合框架?)
谢谢!
$nearSphere
MongoDB 运算符能够返回按递增距离排序的给定点(指定为运算符的参数)附近的文档。
但是,如何获得实际距离?有没有办法使用 MongoDB 功能来获得这个?使用$nearSphere
(尽管我检查了它的文档并且我没有找到此信息)或使用其他机制(可能是聚合框架?)
谢谢!
通过聚合,您可以使用$geoNear阶段来提供球面几何形状并返回距离。
文档中的示例:
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
}
}
])