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

$maxDistance

The $maxDistance operator in Amazon DocumentDB is used to specify the maximum distance (in meters) from a GeoJSON point that documents must be within to be included in the query results. This operator is used in conjunction with the $nearSphere operator to perform geospatial queries.

Parameters

  • $maxDistance: The maximum distance (in meters) from the reference point that documents must be within to be included in the query results.

Example (MongoDB Shell)

The following example demonstrates how to use the $maxDistance operator in Amazon DocumentDB to find all state capitals within 100 kilometers of Boston.

Create sample documents

db.capitals.insert([ { state: "Massachusetts", city: "Boston", location: { type: "Point", coordinates: [-71.0589, 42.3601] } }, { state: "Rhode Island", city: "Providence", location: { type: "Point", coordinates: [-71.4128, 41.8239] } }, { state: "New Hampshire", city: "Concord", location: { type: "Point", coordinates: [-71.5383, 43.2067] } }, { state: "Vermont", city: "Montpelier", location: { type: "Point", coordinates: [-72.5751, 44.2604] } } ]);

Query example

db.capitals.find( { location: { $nearSphere: { $geometry: { type: "Point", coordinates: [-71.0589, 42.3601] }, $maxDistance: 100000 } } }, { state: 1, city: 1, _id: 0 } );

Output

[ { "state": "Rhode Island", "city": "Providence" }, { "state": "New Hampshire", "city": "Concord" } ]

Code examples

To view a code example for using the $maxDistance command, choose the tab for the language that you want to use:

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