$nearSphere - Amazon DocumentDB

$nearSphere

The $nearSphere operator in Amazon DocumentDB is used to find documents that are within a specified distance of a geospatial point. This operator is particularly useful for geo-spatial queries, such as finding all restaurants within a certain radius of a given location.

Parameters

  • $geometry: A GeoJSON object that represents the reference point. Must be a Point object with type and coordinates fields.

  • $minDistance: (optional) The minimum distance (in meters) from the reference point that documents must be.

  • $maxDistance: (optional) The maximum distance (in meters) from the reference point that documents must be.

Example (MongoDB Shell)

In this example, we will find all restaurants within 2 kilometers (2000 meters) of a specific location in Seattle, WA.

Create sample documents

db.usarestaurants.insert([ { name: "Noodle House", location: { type: "Point", coordinates: [-122.3516, 47.6156] } }, { name: "Pike Place Grill", location: { type: "Point", coordinates: [-122.3403, 47.6101] } }, { name: "Seattle Coffee Co.", location: { type: "Point", coordinates: [-122.3339, 47.6062] } } ]);

Query example

db.usarestaurants.find({ location: { $nearSphere: { $geometry: { type: "Point", coordinates: [-122.3516, 47.6156] }, $minDistance: 1, $maxDistance: 2000 } } }, { name: 1 });

Output

{ "_id" : ObjectId("611f3da985009a81ad38e74b"), "name" : "Noodle House" } { "_id" : ObjectId("611f3da985009a81ad38e74c"), "name" : "Pike Place Grill" }

Code examples

To view a code example for using the $nearSphere 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'); const result = await restaurants.find({ location: { $nearSphere: { $geometry: { type: "Point", coordinates: [-122.3516, 47.6156] }, $minDistance: 1, $maxDistance: 2000 } } }, { projection: { name: 1 } }).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 result = list(restaurants.find({ 'location': { '$nearSphere': { '$geometry': { 'type': 'Point', 'coordinates': [-122.3516, 47.6156] }, '$minDistance': 1, '$maxDistance': 2000 } } }, { 'name': 1 })) print(result) client.close() find_nearby_restaurants()