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.
APPLIES TO:
MongoDB vCore
The $maxDistance
operator is used in geospatial queries to specify the maximum distance (in meters) that can exist between two points. It's commonly used with $near
to find locations within a certain radius.
Syntax
The syntax for the $maxDistance
operator is as follows:
{
<location field>: {
$near: {
$geometry: {
type: "Point",
coordinates: [<longitude>, <latitude>]
},
$maxDistance: <distance in meters>
}
}
}
Parameters
Parameter | Type | Description |
---|---|---|
location field |
Field | The field containing the geospatial data |
coordinates |
Array | An array of [longitude, latitude] specifying the center point |
$maxDistance |
Number | Maximum distance in meters from the center point |
Example
Using the stores
collection, let's find all stores within 10 KM of the "VanArsdel Picture Frame Store":
db.stores.find({
location: {
$near: {
$geometry: {
type: "Point",
coordinates: [-141.9922, 16.8331] // VanArsdel Picture Frame Store location
},
$maxDistance: 10000 // 10 kilometers in meters
}
}
},
{
name: 1,
location: 1
})
This query returns stores like:
- First Up Consultants Microphone Bazaar
- Fabrikam Car Accessory Outlet and other stores within the 10 KM radius.
Related content
- Review options for Migrating from MongoDB to Azure Cosmos DB for MongoDB (vCore)
- Read more about Feature compatibility with MongoDB