$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.
- {
- <location field>: {
- $geoWithin: { $center: [ [ <x>, <y> ] , <radius> ] }
- }
- }
Important
If you use longitude and latitude, specify longitude first.
Behavior
The query calculates distances using flat (planar) geometry.
Applications can use $center
without 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
:
- db.places.find(
- { loc: { $geoWithin: { $center: [ [-74, 40.74], 10 ] } } }
- )