$centerSphere
Specifies a circle using spherical geometry for $geoWithin queries, more accurate for Earth-based calculations that account for spherical shape.
Syntax
{ $geoWithin: { $centerSphere: [ [<longitude>, <latitude>], <radius_in_radians> ] } }
Parameters
longitude
numberrequiredThe longitude of the circle's center point
latitude
numberrequiredThe latitude of the circle's center point
radius_in_radians
numberrequiredThe radius of the sphere in radians (divide distance in kilometers by 6371 to convert to radians)
Examples
Find stores within spherical area
Find stores within approximately 1000 kilometers using spherical calculations
Query:
db.stores.find({ location: { $geoWithin: { $centerSphere: [[-82.5543, -65.105], 0.157] } } })
Output:
Stores within spherical area using Earth-accurate distance calculations
Spherical query with field selection
Use spherical geometry to find nearby stores with specific field projection
Query:
db.stores.find({ location: { $geoWithin: { $centerSphere: [[-82.5543, -65.105], 0.157] } } }, { name: 1, location: 1, city: 1 })
Output:
Selected fields from stores within spherical boundary