我已经设置了一个运行良好的地理空间查询,但是我希望获得每个结果的距离。
给出查询:
var query = Query.Near("Location", longitude, latitude);
var places = mongoDb.GetCollection<Place>("places").Find(near);
如何检索“places”中每个元素的距离?
最佳答案
我不知道你是否可以从mongo获取距离,但我过去所做的只是在获得数据后使用Haversine公式来获取距离。
你可以找到我使用的实现 here
编辑:抱歉,您可以从 Mongo 获取距离,但不能使用 Query.Near()。您需要使用集合上的 GeoNear 函数。
mongoDb.GetCollection<Place>("places").GeoNear(Query.Null, latitude, longitude, maxDistance);
这将返回 GeoNearResult<Place>
其 Hits 属性为 IEnumurable<Hit>
它将包含 Distance 属性和包含您的地点对象的文档属性。
关于mongodb - 从近查询中检索距离 "dis"结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8810458/