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

$centerSphere

$centerSphere

New in version 1.8.

The $centerSphere operator is the spherical equivalent of the $center operator. $centerSphere uses spherical geometry to define a circle for use by the $within operator in geospatial queries.

To define the bounds of a query using $centerSphere, you must specify:

  • The center point
  • The angular distance in radians (distance along the surface of the earth).

Consider the following prototype:

db.collection.find( { loc: { $within:
                             { $centerSphere:
                               [ [<long>,<lat>], <distance> / <radius> ]
                             } } } )

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

db.collection.find( { loc: { $within:
                             { $centerSphere:
                                [ [88,30], 10 / 3959 ]
                             } } } )

Changed in version 2.2.3: Before 2.2.3, a geospatial index must exist on a field holding coordinates before using any of the geolocation query operators. After 2.2.3, applications may use geolocation query operators without having a geospatial index; however, geospatial indexes will support much faster geospatial queries than the unindexed equivalents.

Note

A geospatial index must exist on a field and the field must hold coordinates before you can use any of the geolocation query operators.