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 $center
operator specifies a circle using legacy coordinate pairs to be used in $geoWithin
queries. It defines a circle for a geospatial query on a flat, Euclidean plane.
Syntax
The syntax for the $center
operator is as follows:
{
$geoWithin: {
$center: [ [ <x>, <y> ], <radius> ]
}
}
Parameters
Parameter | Type | Description |
---|---|---|
<x> |
number | The x-coordinate of the circle's center point |
<y> |
number | The y-coordinate of the circle's center point |
<radius> |
number | The radius of the circle in the same units as the coordinates |
Example
Let's find all stores within a 50-degree radius of a central point using our stores dataset. This query can help identify stores that are within a certain distance of a specific location.
db.stores.find({
"location": {
$geoWithin: {
$center: [[-112.7858, -29.1866], 50]
}
}
})
This query will return stores like:
{
"_id": "f2a8c190-28e4-4e14-9d8b-0256e53dca66",
"name": "Fabrikam, Inc. | Car Accessory Outlet - West Adele",
"location": {
"lat": -47.2548,
"lon": -38.4071
},
// ... other fields
}
The query searches for stores within a 50-degree radius of the First Up Consultants Microphone Bazaar location, which could be useful for analyzing market coverage or planning delivery routes.
Important
The $center
operator works on a flat, Euclidean plane.
For more accurate Earth-like spherical calculations, use $centerSphere
instead.
The radius is specified in the same units as the coordinate system being used.
Related content
- Review options for Migrating from MongoDB to Azure Cosmos DB for MongoDB (vCore)
- Read more about Feature compatibility with MongoDB