/
centerSphere.txt
62 lines (44 loc) · 1.64 KB
/
centerSphere.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
=============
$centerSphere
=============
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. query:: $centerSphere
Defines a circle for a :term:`geospatial` query that uses spherical
geometry. The query returns documents that are within the bounds of
the circle. You can use the :query:`$centerSphere` operator on both
:term:`GeoJSON` objects and legacy coordinate pairs.
To use :query:`$centerSphere`, specify an array that contains:
- The grid coordinates of the circle's center point, and
- The circle's radius measured in radians. To calculate radians, see
:doc:`/tutorial/calculate-distances-using-spherical-geometry-with-2d-geospatial-indexes`.
.. code-block:: javascript
{
<location field>: {
$geoWithin: { $centerSphere: [ [ <x>, <y> ], <radius> ] }
}
}
.. important::
If you use longitude and latitude, specify **longitude first**.
Behavior
--------
.. |operator| replace:: :query:`$centerSphere`
.. include:: /includes/note-geospatial-index-must-exist.rst
Both :doc:`2dsphere </core/2dsphere>` and :doc:`2d </core/2d>`
geospatial indexes support :query:`$centerSphere`.
Example
-------
The following example queries grid coordinates and returns all
documents within a 10 mile radius of longitude ``88 W`` and latitude
``30 N``. The query converts the distance to radians by dividing by the
approximate equatorial radius of the earth, 3963.2 miles:
.. code-block:: javascript
db.places.find( {
loc: { $geoWithin: { $centerSphere: [ [ -88, 30 ], 10/3963.2 ] } }
} )