MongoDB geospatial difference between $near and $within

三世轮回 提交于 2019-12-18 17:27:37

问题


What is the difference between $near and $within?

db.geodata.find({ "loc" : { "$within" : { "$center" : [ [ 12.91365 , 77.59395] , 4]}}}).limit(10);

db.geodata.find({ "loc" : { "$near" : [ 12.91365 , 77.59395] , "$maxDistance" : 4}}).limit(10); 

Can anyone explain in detail?


回答1:


The main differences are

  • $near sorts based on distance from a point; $geoWithin tests for containment in a polygon or multipolygon with GeoJSON coordinates, or containment in one of a set of shapes for 2d coordinates
  • $near returns document from nearest to farthest and any other order requires in-memory sorting; $geoWithin can be used with other sort indexes
  • $near requires a geospatial index; $geoWithin performs better with one but does not require it
  • $near is not supported in sharded clusters - you have to use the geonear command or$geoNear aggregation stage instead

Also check out the documentation for $near and $geoWithin.



来源:https://stackoverflow.com/questions/28460960/mongodb-geospatial-difference-between-near-and-within

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