$minDistance

Definition

  • $minDistance

New in version 2.6.

Filters the results of a geospatial $near or$nearSphere query to those documents that are at least thespecified distance from the center point.

If $near or $nearSphere query specifies the centerpoint as a GeoJSON point, specify the distanceas a non-negative number in meters.

If $nearSphere query specifies the center point aslegacy coordinate pair, specifythe distance as a non-negative number in radians. $nearcan only use the 2dsphere index if the queryspecifies the center point as a GeoJSON point.

Examples

Use with $near

Important

If specifying latitude and longitude coordinates, list thelongitude first and then latitude:

  • Valid longitude values are between -180 and 180, bothinclusive.
  • Valid latitude values are between -90 and 90, bothinclusive.

Consider a collection places that has a 2dsphere index.

The following example returns documents that are at least 1000meters from and at most 5000 meters from the specified GeoJSONpoint, sorted from nearest to farthest:

  1. db.places.find(
  2. {
  3. location:
  4. { $near :
  5. {
  6. $geometry: { type: "Point", coordinates: [ -73.9667, 40.78 ] },
  7. $minDistance: 1000,
  8. $maxDistance: 5000
  9. }
  10. }
  11. }
  12. )

Use with $nearSphere

Consider a collection places that contains documents with alocation field and has a 2dsphere index.

Then, the following example returns whose location is at least1000 meters from and at most 5000 meters from the specifiedpoint, ordered from nearest to farthest:

  1. db.places.find(
  2. {
  3. location: {
  4. $nearSphere: {
  5. $geometry: {
  6. type : "Point",
  7. coordinates : [ -73.9667, 40.78 ]
  8. },
  9. $minDistance: 1000,
  10. $maxDistance: 5000
  11. }
  12. }
  13. }
  14. )

For an example that specifies the center point as legacy coordinatepair, see $nearSphere