OPTIONS
翻译或纠错本页面

$center

$center

1.4 新版功能.

The $center operator specifies a circle for a geospatial $geoWithin query. The query returns legacy coordinate pairs that are within the bounds of the circle. The operator does not return GeoJSON objects.

The query calculates distances using flat (planar) geometry.

The 2d geospatial index supports the $center operator.

To use the $center operator, specify an array that contains:

  • The grid coordinates of the circle’s center point
  • The circle’s radius, as measured in the units used by the coordinate system

重要

If you use longitude and latitude, specify longitude first.

Use the following syntax:

{ <location field> : { $geoWithin : { $center : [ [ <x>, <y> ] , <radius> ] } } }

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

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

在 2.2.3 版更改: Applications can use $center 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.

ON THIS PAGE