Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The $maxDistance
operator is used in geospatial queries to specify the maximum distance (in meters) that can exist between two points. It pairs well with $near
for radius-based location searches.
{
<location field>: {
$near: {
$geometry: {
type: "Point",
coordinates: [<longitude>, <latitude>]
},
$maxDistance: <distance in meters>
}
}
}
Parameter | Description |
---|---|
location field |
The field containing the geospatial data |
coordinates |
An array of [longitude, latitude] specifying the center point |
$maxDistance |
Maximum distance in meters from the center point |
Using the stores
collection, let's find all stores within 10Km of the Point coordinate.
db.stores.find({
location: {
$near: {
$geometry: {
type: "Point",
coordinates: [-77.9951,-62.7339]
},
$maxDistance: 10000 // 10 kilometers in meters
}
}
},
{
name: 1,
location: 1
}).limit(2)
The query returns only one store that falls within 10Km of the coordinate provided.
{
"_id": "66fd4cdd-ffc3-44b6-81d9-6d5e9c1f7f9a",
"name": "Trey Research | Health Food Center - North Michelle",
"location": { "lat": -77.9951, "lon": -62.7339 }
}
- Review options for migrating from MongoDB to Azure Cosmos DB for MongoDB (vCore).
- Read more about feature compatibility with MongoDB.