问题描述
使用MongoDB,我正在查询经纬度25英里以内的房屋.
Using MongoDB I'm querying homes that are within 25 miles of a lat/long.
我第一次尝试执行此操作是使用Near命令,如下所示:
My first attempt to do this used the near command, like so:
var near = Query.Near("Coordinates", coordinates.Latitude, coordinates.Longitude, find.GetRadiansAway(), false);
var query = Collection().Find(near);
var listings = query.ToList();
Near的问题是它仅返回100个列表,而我想返回距坐标25英里以内的所有列表.
The issue with near is that it only returns 100 listings, whereas I want to return all listings within 25 miles of the coordinates.
我的下一个尝试是在以下范围内使用
My next attempt was to use within:
var within = Query.WithinCircle("Coordinates", coordinates.Latitude, coordinates.Longitude, find.GetRadiansAway(), false);
var query = Collection().Find(within);
var listings = query.ToList();
Within返回25英里内的所有列表,这很好,但是并不会像Near那样根据它们与中心坐标的接近程度对它们进行排序.
Within returns all listings within 25 miles, which is great, however it doesn't sort them by how close they are to the center coordinates like near does.
所以我的问题是,我如何才能做到两全其美?如何获得25英里范围内的所有列表,并按照与中心坐标的接近程度对其进行排序?
So my question is, how do I get the best of both worlds? How do I get all listings within 25 miles AND have them sorted by proximity to the center coordinates?
推荐答案
地理空间$near
查询设置的默认limit()
为100个结果.通过设置新的limit()
,您应该可以获得更多结果.
Geospatial $near
queries set a default limit()
of 100 results. You should be able to get more results by setting a new limit()
.
近"查询是按距离排序的,而内"则不是(尽管内"没有默认限制).
While "near" queries are sorted by distance, "within" is not (although "within" doesn't have a default limit).
这篇关于查询附近还是内部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!