MongoDB geospatial difference between $near and $within

后端 未结 1 862
名媛妹妹
名媛妹妹 2021-01-03 06:23

What is the difference between $near and $within?

db.geodata.find({ \"loc\" : { \"$within\" : { \"$center\" : [ [ 12.91365 , 77.59         


        
相关标签:
1条回答
  • 2021-01-03 07:00

    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.

    0 讨论(0)
提交回复
热议问题