$minDistance
$minDistance is a find operator used in conjunction with $nearSphere or $geoNear to filter documents that are at least at the specified minimum distance from the center point. This operator is supported in Amazon DocumentDB and functions similarly to its counterpart in MongoDB.
Parameters
-
$minDistance: The minimum distance (in meters) from the center point to include documents in the results.
Example (MongoDB Shell)
In this example, we'll find all restaurants within a 2-kilometer radius of a specific location in Seattle, Washington.
Create sample documents
db.usarestaurants.insertMany([ { "state": "Washington", "city": "Seattle", "name": "Noodle House", "rating": 4.8, "location": { "type": "Point", "coordinates": [-122.3517, 47.6159] } }, { "state": "Washington", "city": "Seattle", "name": "Pike Place Grill", "rating": 4.5, "location": { "type": "Point", "coordinates": [-122.3412, 47.6102] } }, { "state": "Washington", "city": "Bellevue", "name": "The Burger Joint", "rating": 4.2, "location": { "type": "Point", "coordinates": [-122.2007, 47.6105] } } ]);
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 $minDistance command, choose the tab for the language that you want to use: