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