Skip to content

Commit cff05b3

Browse files
author
Bob Grabar
committed
DOCS-934 centerSphere and within
1 parent 85cf9a0 commit cff05b3

File tree

5 files changed

+71
-24
lines changed

5 files changed

+71
-24
lines changed

source/core/geospatial-indexes.txt

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@
77
Overview
88
--------
99

10-
``2d`` geospatial indexes support efficient queries using
11-
location-based data in a document, and special geospatial query
12-
operators. You can store two-dimensional location coordinates in
13-
documents and with a geospatial index on this field, construct
14-
location-based queries. For example, you can query for documents based
10+
Geospatial indexing allows you to associate a document with a location
11+
in two-dimensional space, such as a point on a map. You store
12+
two-dimensional coordinates in a location field in your documents,
13+
create a geospatial index on that field, and construct location-based
14+
queries. Geospatial indexes provide special geospatial query operators.
15+
For example, you can query for documents based
1516
on proximity to another location or based on inclusion in a specified
1617
region.
1718

18-
Additionally, geospatial indexes support queries on both the
19-
coordinate field *and* another field. For example, you might write a
19+
Geospatial indexes support queries on both the coordinate field *and*
20+
another field, such as a type of business or attraction. For example,
21+
you might write a
2022
query to find restaurants a specific distance from a hotel or to find
21-
museums found within a certain defined neighborhood.
23+
museums within a certain defined neighborhood.
2224

23-
This document describes how to include location data in your documents
25+
This document describes how to store location data in your documents
2426
and how to create geospatial indexes. For information on querying data
2527
stored in geospatial indexes, see :doc:`/applications/geospatial-indexes`.
2628

@@ -31,17 +33,17 @@ Store Location Data
3133

3234
To use ``2d`` geospatial indexes, you must model location data on a
3335
predetermined two-dimensional coordinate system, such as longitude
34-
and latitude. You store location data as two-dimensional coordinates
36+
and latitude. You store a document's location data as two coordinates
3537
in a field that holds either a two-dimensional array or an embedded
36-
document. Consider the following two examples:
38+
document with two fields. Consider the following two examples:
3739

3840
.. code-block:: javascript
3941

4042
loc : [ x, y ]
4143

4244
loc : { x: 1, y: 2 }
4345

44-
All documents must store location data in the same order; however, if
46+
All documents must store location data in the same order. If
4547
you use latitude and longitude as your coordinate system, always store
4648
longitude first. MongoDB's :ref:`2d spherical index operators
4749
<geospatial-indexes-spherical>` only recognize ``[ longitude, latitude
@@ -63,7 +65,7 @@ location field of your collection. Consider the following prototype:
6365

6466
db.collection.ensureIndex( { <location field> : "2d" } )
6567

66-
MongoDB's special :ref:`geospatial operations
68+
MongoDB's :ref:`geospatial operations
6769
<geospatial-query-operators>` use this index when querying for location
6870
data.
6971

@@ -87,12 +89,12 @@ Location Range
8789
~~~~~~~~~~~~~~
8890

8991
All ``2d`` geospatial indexes have boundaries defined by a coordinate
90-
range. By default, ``2s`` geospatial indexes assume longitude and
92+
range. By default, ``2d`` geospatial indexes assume longitude and
9193
latitude have boundaries of -180 inclusive and 180 non-inclusive
9294
(i.e. ``[-180, 180)``). MongoDB returns an error and rejects documents
9395
with coordinate data outside of the specified range.
9496

95-
To build an index with a different location range other than the
97+
To build an index with a location range other than the
9698
default, use the ``min`` and ``max`` options with the
9799
:method:`ensureIndex() <db.collection.ensureIndex()>` operation when
98100
creating a ``2d`` index, as in the following prototype:
@@ -253,8 +255,8 @@ for geospatial information based on a sphere or earth.
253255
.. admonition:: Spherical Queries Use Radians for Distance
254256

255257
For spherical operators to function properly, you must convert
256-
distances to radians, and convert from radians to distances units
257-
for your application.
258+
distances to radians, and convert from radians to the distances units
259+
used by your application.
258260

259261
To convert:
260262

source/includes/note-geospatial-index-must-exist.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@
55
operators *without* having a geospatial index; however, geospatial
66
indexes will support much faster geospatial queries than the
77
unindexed equivalents.
8+
9+
.. note::
10+
11+
A geospatial index *must* exist on a field and the field must hold coordinates
12+
before you can use any of the geolocation query operators.

source/reference/operator/centerSphere.txt

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,26 @@ $centerSphere
1010

1111
The :operator:`$centerSphere` operator is the spherical equivalent
1212
of the :operator:`$center` operator. :operator:`$centerSphere` uses
13-
spherical geometry to calculate distances in a circle specified by
14-
a point and radius.
13+
spherical geometry to define a circle for use by the
14+
:operator:`$within` operator in :term:`geospatial` queries.
1515

16-
Considering the following example:
16+
To define the bounds of a query using :operator:`$centerSphere`, you
17+
must specify:
1718

18-
.. code-block:: javascript
19+
- The center point
20+
21+
- The radian. To calculate the radian, divide the radius of
22+
the circle by the radius of the sphere.
1923

2024
db.collection.find( { loc: { $within: { $centerSphere: [ [0,0], 10 / 3959 ] } } } )
2125

22-
This query will return all documents within a 10 mile radius of
23-
``[0,0]`` using a spherical geometry to calculate distances.
26+
The following example returns all documents within a 10 mile radius
27+
of longitude ``88 W`` and latitude ``30 N``. The document determines
28+
the radian by dividing 10 miles by the approximate radius of the earth:
29+
3960 miles.
30+
31+
.. code-block:: javascript
32+
33+
db.collection.find( { loc: { $within: { $centerSphere: [ [ 88 , 30 ], 10 / 3960 ] } } } )
2434

2535
.. include:: /includes/note-geospatial-index-must-exist.rst

source/reference/operator/within.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ $within
1515
db.collection.find( { location: { $within: { shape } } } );
1616

1717
Replace ``{ shape }`` with a document that describes a shape. The
18-
:operator:`$within` command supports three shapes. These shapes and the
18+
:operator:`$within` command supports the following shapes. These shapes and the
1919
relevant expressions follow:
2020

2121
- Rectangles. Use the :operator:`$box` operator, consider the following
@@ -35,6 +35,9 @@ $within
3535

3636
db.collection.find( { location: { $within: { $center: [ center, radius } } } );
3737

38+
- Circles measured by spherical geometry. Use the
39+
:operator:`$centerSphere` operator. For the syntax, see :operator:`$centerSphere`.
40+
3841
- Polygons. Use the :operator:`$polygon` operator. Specify polygons with an array of points. See the
3942
following example:
4043

source/reference/operators.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,15 @@ Geospatial
105105
- :operator:`$near`
106106
- :operator:`$within`
107107

108+
.. include:: operator/near.txt
109+
:start-after: mongodb
110+
111+
.. include:: operator/nearSphere.txt
112+
:start-after: mongodb
113+
114+
.. include:: operator/within.txt
115+
:start-after: mongodb
116+
108117
Use the following operators within the context of
109118
:operator:`$within`:
110119

@@ -115,6 +124,24 @@ Use the following operators within the context of
115124
- :operator:`$polygon`
116125
- :operator:`$uniqueDocs`
117126

127+
.. include:: operator/maxDistance.txt
128+
:start-after: mongodb
129+
130+
.. include:: operator/center.txt
131+
:start-after: mongodb
132+
133+
.. include:: operator/centerSphere.txt
134+
:start-after: mongodb
135+
136+
.. include:: operator/box.txt
137+
:start-after: mongodb
138+
139+
.. include:: operator/polygon.txt
140+
:start-after: mongodb
141+
142+
.. include:: operator/uniqueDocs.txt
143+
:start-after: mongodb
144+
118145
.. index:: query selectors; array
119146
.. _query-selectors-array:
120147

0 commit comments

Comments
 (0)