$minDistance - 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).

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

Node.js
const { MongoClient } = require('mongodb'); async function findRestaurantsNearby() { 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 collection = db.collection('usarestaurants'); const result = await collection.find({ "location": { "$nearSphere": { "$geometry": { "type": "Point", "coordinates": [-122.3516, 47.6156] }, "$minDistance": 1, "$maxDistance": 2000 } } }, { "projection": { "name": 1 } }).toArray(); console.log(result); client.close(); } findRestaurantsNearby();
Python
from pymongo import MongoClient def find_restaurants_nearby(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client.test collection = db.usarestaurants result = list(collection.find({ "location": { "$nearSphere": { "$geometry": { "type": "Point", "coordinates": [-122.3516, 47.6156] }, "$minDistance": 1, "$maxDistance": 2000 } } }, { "projection": {"name": 1} })) print(result) client.close() find_restaurants_nearby()