$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: