$near (geospatial)

APPLIES TO: MongoDB vCore

The $near operator returns documents whose location field is near a specified point, sorted by distance. It requires a '2dsphere' index and returns documents from nearest to farthest.

Syntax

{
  <location field>: {
    $near: {
      $geometry: {
        type: "Point",
        coordinates: [<longitude>, <latitude>]
      },
      $maxDistance: <distance in meters>,
      $minDistance: <distance in meters>
    }
  }
}

Parameters

Parameter Type Description
location field Field The field containing the GeoJSON Point
$geometry Object GeoJSON Point object specifying the center point
$maxDistance Number Optional. Maximum distance in meters from the center point
$minDistance Number Optional. Minimum distance in meters from the center point

Examples

Create the required '2dsphere' index:

db.stores.createIndex({ "location": "2dsphere" })

Find stores near the Proseware Home Entertainment Hub location (70.1272, 69.7296), sorted by distance:

db.stores.find({
  'location': {
    $near: {
      $geometry: {
        type: "Point",
        coordinates: [69.7296, 70.1272]  // [longitude, latitude]
      }
    }
  }
}, {
  name: 1,
  location: 1
})

Example 2: Using Maximum Distance

Find stores within 10 KM of First Up Consultants Microphone Bazaar (-112.7858, -29.1866):

db.stores.find({
  'location': {
    $near: {
      $geometry: {
        type: "Point",
        coordinates: [-112.7858, -29.1866]
      },
      $maxDistance: 10000  // 10 kilometers in meters
    }
  }
}, {
  name: 1,
  location: 1
})

Example 3: Using Both Min and Max Distance

Find stores between 10 and 50 KM from Wide World Importers (-82.5543, -65.105):

db.stores.aggregate([
  {
    $geoNear: {
      near: {
        type: "Point",
        coordinates: [-82.5543, -65.105]
      },
      distanceField: "distance",
      minDistance: 10000,    // 10 KM in meters
      maxDistance: 50000,    // 50 KM in meters,
      spherical: true
    }
  },
  {
    $project: {
      name: 1,
      location: 1,
      distanceKm: { $divide: ["$distance", 1000] },
      _id: 0
    }
  }
])

This returns stores with their distances from the specified point, such as:

  • Distance to nearby stores in kilometers
  • Only stores within the specified distance range
  • Sorted from nearest to farthest