$center

Definition

  • $center
  • The $center operator specifies a circle for a$geoWithin query. The query returns legacy coordinate pairsthat are within the bounds of the circle. The operator does _not_return GeoJSON objects.

To use the $center operator, specify an array thatcontains:

  • The grid coordinates of the circle’s center point, and
  • The circle’s radius, as measured in the units used by thecoordinate system.
  1. {
  2. <location field>: {
  3. $geoWithin: { $center: [ [ <x>, <y> ] , <radius> ] }
  4. }
  5. }

Important

If you use longitude and latitude, specify longitude first.

Behavior

The query calculates distances using flat (planar) geometry.

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

Only the 2d geospatial index supports$center.

Example

The following example query returns all documents that have coordinatesthat exist within the circle centered on [ -74, 40.74 ] and with aradius of 10:

  1. db.places.find(
  2. { loc: { $geoWithin: { $center: [ [-74, 40.74], 10 ] } } }
  3. )