$near - Amazon DocumentDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

$near

The $near operator in Amazon DocumentDB is used to find documents that are geographically near a specified point. It returns documents ordered by distance, with the closest documents first. This operator requires a 2dsphere geospatial index and is useful for proximity queries on location data.

Parameters

  • $geometry: A GeoJSON Point object that defines the center point for the near query.

  • $maxDistance: (optional) The maximum distance in meters from the specified point that a document can be to match the query.

  • $minDistance: (optional) The minimum distance in meters from the specified point that a document can be to match the query.

Index Requirements

  • 2dsphere index: Required for geospatial queries on GeoJSON Point data.

Example (MongoDB Shell)

The following example demonstrates how to use the $near operator to find the nearest restaurants to a specific location in Seattle, Washington.

Create sample documents

db.usarestaurants.insert([ { "name": "Noodle House", "city": "Seattle", "state": "Washington", "rating": 4.8, "location": { "type": "Point", "coordinates": [-122.3517, 47.6159] } }, { "name": "Pike Place Grill", "city": "Seattle", "state": "Washington", "rating": 4.2, "location": { "type": "Point", "coordinates": [-122.3403, 47.6062] } }, { "name": "Lola", "city": "Seattle", "state": "Washington", "rating": 4.5, "location": { "type": "Point", "coordinates": [-122.3407, 47.6107] } } ]);

Create 2dsphere index

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

Query example with GeoJSON Point

db.usarestaurants.find({ location: { $near: { $geometry: { type: "Point", coordinates: [-122.3516, 47.6156] }, $maxDistance: 100, $minDistance: 10 } } });

Output

{ "_id" : ObjectId("69031ec9ea1c2922a1ce5f4a"), "name" : "Noodle House", "city" : "Seattle", "state" : "Washington", "rating" : 4.8, "location" : { "type" : "Point", "coordinates" : [ -122.3517, 47.6159 ] } }

Code examples

To view a code example for using the $near command, choose the tab for the language that you want to use:

Node.js
const { MongoClient } = require('mongodb'); async function findNearbyRestaurants() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const restaurants = db.collection('usarestaurants'); // Create 2dsphere index await restaurants.createIndex({ "location": "2dsphere" }); const result = await restaurants.find({ location: { $near: { $geometry: { type: "Point", coordinates: [-122.3516, 47.6156] }, $maxDistance: 100, $minDistance: 10 } } }).toArray(); console.log(result); client.close(); } findNearbyRestaurants();
Python
from pymongo import MongoClient def find_nearby_restaurants(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client['test'] restaurants = db['usarestaurants'] # Create 2dsphere index restaurants.create_index([("location", "2dsphere")]) result = list(restaurants.find({ 'location': { '$near': { '$geometry': { 'type': 'Point', 'coordinates': [-122.3516, 47.6156] }, '$maxDistance': 100, '$minDistance': 10 } } })) print(result) client.close() find_nearby_restaurants()