$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 aPointobject withtypeandcoordinatesfields. -
$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: