Navigation
This version of the documentation is archived and no longer supported.

$centerSphere

$centerSphere

New in version 1.8.

The $centerSphere operator defines a circle for a geospatial query that uses spherical geometry. The query returns documents that are within the bounds of the circle.

You can use the $centerSphere operator on both GeoJSON objects and legacy coordinate pairs.

The 2d and 2dsphere geospatial indexes both support $centerSphere.

To use $centerSphere, specify an array that contains:

Use the following syntax:

db.<collection>.find( { <location field> :
                         { $geoWithin :
                            { $centerSphere : [ [ <x>, <y> ] , <radius> ] }
                      } } )

Important

If you use longitude and latitude, specify longitude first.

The following example queries grid coordinates and returns all documents within a 10 mile radius of longitude 88 W and latitude 30 N. The query converts the distance to radians by dividing by the approximate radius of the earth, 3959 miles:

db.places.find( { loc : { $geoWithin :
                           { $centerSphere :
                             [ [ -88 , 30 ] , 10 / 3959 ]
                } } } )

Changed in version 2.2.3: Applications can use $centerSphere without having a geospatial index. However, geospatial indexes support much faster queries than the unindexed equivalents. Before 2.2.3, a geospatial index must exist on a field holding coordinates before using any of the geospatial query operators.

←   $center $box  →