Query near vs. within

随声附和 提交于 2019-12-01 03:58:42

问题


Using MongoDB I'm querying homes that are within 25 miles of a lat/long.

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();

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 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.

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?


回答1:


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).



来源:https://stackoverflow.com/questions/5492901/query-near-vs-within

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!