$centerSphere

Definition

  • $centerSphere
  • Defines a circle for a geospatial query that uses sphericalgeometry. The query returns documents that are within the bounds ofthe circle. You can use the $centerSphere operator on bothGeoJSON objects and legacy coordinate pairs.

To use $centerSphere, specify an array that contains:

  1. {
  2. <location field>: {
  3. $geoWithin: { $centerSphere: [ [ <x>, <y> ], <radius> ] }
  4. }
  5. }

Important

If you use longitude and latitude, specify longitude first.

Behavior

Applications can use $centerSpherewithout having a geospatial index.However, geospatial indexes support much faster queries than theunindexed equivalents.

Both 2dsphere and 2dgeospatial indexes support $centerSphere.

Example

The following example queries grid coordinates and returns alldocuments within a 10 mile radius of longitude 88 W and latitude30 N. The query converts the distance to radians by dividing by theapproximate equatorial radius of the earth, 3963.2 miles:

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