$maxDistance

Definition

  • $maxDistance
  • The $maxDistance operator constrains the results of ageospatial $near or $nearSphere query to thespecified distance. The measuring units for the maximum distance aredetermined by the coordinate system in use. For GeoJSON pointobject, specify the distance in meters, not radians.

Changed in version 2.6: Specify a non-negative number for $maxDistance.

The 2dsphere and 2dgeospatial indexes both support $maxDistance: .

Example

The following example query returns documents with location values thatare 10 or fewer units from the point [ -74 , 40 ].

  1. db.places.find( {
  2. loc: { $near: [ -74 , 40 ], $maxDistance: 10 }
  3. } )

MongoDB orders the results by their distance from [ -74 , 40 ].The operation returns the first 100 results, unless you modify thequery with the cursor.limit() method.