Skip to content

Commit 4d68967

Browse files
author
Andrew Leung
committed
re-editing geospatial core document
1 parent 0ac39fe commit 4d68967

File tree

2 files changed

+103
-87
lines changed

2 files changed

+103
-87
lines changed

draft/applications/geospatial-indexes.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ please see the :ref:`geospatial-query-distance` section.
167167
queries with ``num`` option will only return the specified number
168168
of documents unsorted.
169169

170+
.. note::
171+
172+
Limits in geospatial queries are always applied to the geospatial
173+
component first. This will affect the results if you specify
174+
additional sort operations.
175+
170176
.. index:: geospatial queries; distance limit
171177

172178
.. _geospatial-query-distance:

draft/core/geospatial-indexes.txt

Lines changed: 97 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ Overview
99

1010
MongoDB supports location-based queries and geospatial data with a
1111
special index for coordinate data. The geospatial index stores :ref:`geohashes
12-
<geospatial-geohash>`, and makes it possible to return documents
13-
based on proximity to a point or within a bounded region. MongoDB can
14-
calculate distances between points using flat or spherical geometry.
12+
<geospatial-geohash>`, and makes it possible to query documents
13+
based on proximity or within a bounded region. MongoDB can
14+
calculate distances using flat or spherical geometry.
1515
Additionally, geospatial haystack indexes provide additional
16-
support for certain classes of region-based queries.
16+
support for tuning indexes.
1717

18-
This document introduces the core concepts
19-
that underpin geospatial queries and indexes in MongoDB. For more information,
18+
.. TODO double check haystack benefits.
19+
20+
This document introduces the core concepts that underpin geospatial
21+
indexes in MongoDB. For more information,
2022
:doc:`/applications/geospatial-indexes` provide complete documentation
2123
of all location-based operations and queries.
2224

@@ -30,59 +32,60 @@ Geospatial Indexes
3032
.. see:: :ref:`geospatial-coordinates` for an overview on modeling
3133
location data in MongoDB.
3234

33-
To use geospatial functions in MongoDB, model
34-
location data in a 2D array, then create an index using the
35-
:func:`ensureIndex <db.collection.ensureIndex()>` method on this field
36-
using the ``2d`` option. Consider the following prototype operation:
35+
To use geospatial operators in MongoDB, use the :func:`ensureIndex
36+
<db.collection.ensureIndex()>` method with the ``2d`` value on
37+
location field of your collection. Consider the following prototype
38+
operation:
3739

3840
.. code-block:: javascript
3941

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

42-
After the ``2d`` index has been created, all queries using coordinates
43-
will use this geospatial index. The index uses a :term:`geohash`
44-
calculated from the coordinates. For more information on
45-
:term:`geohash`, please refer to the :ref:`geohash
46-
<geospatial-geohash>` section.
44+
Almost all geospatial operators will query this index for location
45+
data. The index comprises of :term:`geohashes <geohash>` calculated
46+
from location values and the index's geospatial :ref:`range
47+
<geospatial-indexes-range`. For more information on :term:`geohash`,
48+
please refer to the :ref:`geohash <geospatial-geohash>` section.
4749

4850
To perform queries on your geospatial data, please see
4951
:doc:`/applications/geospatial-indexes`.
5052

5153
.. note::
5254

53-
MongoDB only supports *one* geospatial index per collection. As
54-
with any MongoDB index, any single query can only
55-
use one index. Creating more than one geospatial index with a
55+
MongoDB only supports *one* geospatial index per
56+
collection. Creating more than one geospatial index with a
5657
collection will produce unexpected results.
5758

5859
.. _geospatial-indexes-range:
5960

6061
Range
6162
~~~~~
6263

63-
All geospatial indexes are bounded. MongoDB will
64-
return an error and reject documents with coordinate data outside of
65-
the specified range. The default range assumes latitude and longitude
66-
data, and are between -180 inclusive and 180
67-
non-inclusive (i.e. ``[-180, 180)``.)
64+
All geospatial indexes are bounded to a range as this range is used to
65+
compute :term:`geohash` values. MongoDB will return an error and
66+
reject documents with coordinate data outside of the specified range,
67+
because the :term:`geohash` value will be invalid. The default range
68+
assumes latitude and longitude data, and are between -180 inclusive
69+
and 180 non-inclusive (i.e. ``[-180, 180)``.)
6870

6971
To configure the range of a geospatial index, use the ``min`` and
7072
``max`` options with the :func:`ensureIndex() <db.collection.ensureIndex()>`
71-
operation, as in the following prototype.
73+
operation, as in the following prototype:
7274

7375
.. code-block:: javascript
7476

7577
db.collection.ensureIndex( { <location field>: "2d" } ,
7678
{ min: <lower bound> , max: <upper bound> } )
7779

78-
The following operation will create an index on the ``places``
79-
collection, for coordinates in the ``loc`` field, with the range
80-
between ``-90`` and ``90``:
80+
.. TODO this might not be necessary...
81+
82+
The following example command will create a geospatial index with a
83+
range between ``-90`` and ``90``:
8184

8285
.. code-block:: javascript
8386

8487
db.places.ensureIndex( { loc: "2d" } ,
85-
{ min: 90 , max: 90 } )
88+
{ min: -90 , max: 90 } )
8689

8790
For more information see the :ref:`geospatial precision
8891
<geospatial-indexes-precision>` and :ref:`geohash
@@ -93,57 +96,63 @@ For more information see the :ref:`geospatial precision
9396
Precision
9497
~~~~~~~~~
9598

96-
Geospatial indexes use "bits" to represent precision, or
97-
resolution. Higher bit values allow MongoDB to return more precise
98-
results, at the expense of speed. For more information on the
99+
Geospatial indexes consist of :term:`geohash` values and the number of
100+
bits determine the precision of the index. More bits create more
101+
precise :term:`geohash` values, which allow MongoDB to return more
102+
precise results. Fewer bits create a shorter :term:`geohash` value,
103+
which allow for faster processing. For more information on the
99104
relationship between bits and precision, see the :ref:`geohash
100105
<geospatial-geohash>` section.
101106

102-
By default, geospatial indexes in MongoDB have 26 bits of precision,
103-
although precision is configurable upon index creation and MongoDB
104-
supports up to 32 bits of precision. With 26 bits of precision, using
105-
the default range of -180 to 180, geospatial data can be precise to roughly 2
106-
feet or about 60 centimeters.
107+
The precision of geospatial indexes can be configured upto 32 bits. By
108+
default, geospatial indexes use 26 bits of precision, which is precise
109+
to roughly 2 feet or about 60 centimeters using the default range of
110+
-180 to 180.
107111

108-
You can set the precision of a geospatial index during creation by
112+
The precision of a geospatial index can be configured during creation by
109113
specifying the ``bits`` option to the :func:`ensureIndex()
110114
<db.command.ensureIndex()>` method, as in the following prototype:
111115

116+
.. TODO question: would the index recalculate geohashes if we
117+
.. reconfigure the bits option after the index has been made? or do we
118+
.. need to drop the index?
119+
112120
.. code-block:: javascript
113121

114122
db.collection.ensureIndex( {<location field>: "2d"} ,
115123
{ bits: <bit precision> } )
116124

117-
Only create an index with fewer than 26 bits *if* your the data in
118-
your collection is less precise and/or you're willing to sacrifice
125+
Only create an index with fewer than 26 bits *if* the data in your
126+
collection is less precise and/or you're willing to sacrifice
119127
precision for query speed.
120128

121129
Compound Indexes
122130
~~~~~~~~~~~~~~~~
123131

124-
MongoDB supports :ref:`compound indexes <index-type-compound>` where
125-
one component holds coordinate data. For queries that include these
126-
fields, MongoDB will use this index for a larger portion of these
127-
operations, which will improve performance for these queries.
132+
:ref:`Compound indexes <index-type-compound>` can be used with
133+
geospatial data to improve performance that query all these fields.
128134

129-
Use an operation that resembles the following to create a geospatial
130-
index with a compound key.
135+
To create compound index with geospatial data and another field, the
136+
prototype form is:
131137

132138
.. code-block:: javascript
133139

134140
db.collection.ensureIndex( { <location field>: "2d", <field>: 1 } );
135141

136-
These indexes support regular geospatial queries as well as queries where you must filter both by
137-
location and by another field. For example, if you need to return a
138-
list of restaurants near a given point, but you want to optionally
139-
filter restaurants by type, such as "take-out," or
140-
"bar" stored in the ``type`` field.
142+
This index will now support regular geospatial queries as well as
143+
queries where you must filter both by location and by another field.
141144

142-
.. note::
145+
For example, to create a compound index on the ``loc`` field and the
146+
``type`` field, the command is:
147+
148+
.. code-block:: javascript
143149

144-
Limits in geospatial queries are always applied to the geospatial
145-
component first. This will affect the results if you specify
146-
additional sort operations.
150+
db.storeInfo.ensureIndex( { loc: "2d", type: 1 } );
151+
152+
This will support geospatial queries as well as any query on the
153+
``type`` field. This index can return a list of restaurants near a
154+
given point, which can optionally filter restaurants by the ``type``,
155+
such as "take-out," or "bar".
147156

148157
.. see also: ":ref:`<index-type-compound>`" and ":ref:`<geospatial-haystack-index>`"
149158

@@ -152,37 +161,39 @@ filter restaurants by type, such as "take-out," or
152161
Haystack Index
153162
~~~~~~~~~~~~~~
154163

155-
Geospatial haystack indexes make it possible to tune the
156-
distribution of your data and build a special bucket
157-
index. Haystack indexes improve query performance for queries limited
158-
to a specific area.
164+
Geospatial haystack indexes make it possible to tune the index to the
165+
distribution of your data and build a special bucket index. Haystack
166+
indexes improve query performance for queries limited to a specific
167+
area.
159168

160169
.. note::
161170

162-
Haystack indexes are not suited to returning the closest documents to
163-
a particular location, as the closest documents could be far away
164-
compared to the ``bucketSize``.
171+
Haystack indexes are tuned to the ``bucketSize`` and are not suited
172+
to returning the closest documents to a particular location,
173+
because the closest documents could be further away than to the
174+
``bucketSize``.
165175

166-
To build a :term:`geoHaystack` index, specify the ``geoHaystack`` for the
167-
location field and a ``bucketSize`` parameter . The ``bucketSize``
168-
parameter determines the granularity of the bucket index. A
169-
``bucketSize`` of ``1`` creates an index that stores keys within a single unit
170-
in the coordinate in the same bucket.
176+
The following command is the prototype to build a :term:`geoHaystack`
177+
index:
171178

172179
.. code-block:: javascript
173180

174181
db.collection.ensureIndex({ <location field>: "geoHaystack", type: 1 },
175182
{ bucketSize: <bucket value> })
176183

177-
By default, all queries that use a geospatial haystack index will return 50
178-
documents.
184+
The ``bucketSize`` parameter determines the granularity of the
185+
index. A bucket value of ``5`` creates an index that stores keys
186+
within 5 units of the coordinate in the same bucket.
179187

180-
To query geohaystack indexes, you *must* use the :dbcommand:`geoSearch`
188+
Geohaystack indexes are only queried by the :dbcommand:`geoSearch`
181189
command, please see the :ref:`Querying Haystack Indexes
182-
<geospatial-haystack-queries>` section.
190+
<geospatial-haystack-queries>` section for command details.
183191

184192
:ref:`Spherical queries <geospatial-spherical-geometry>` are not
185-
supported by haystack indexes.
193+
supported by geohaystack indexes.
194+
195+
By default, all queries that use a geospatial haystack index will return 50
196+
documents.
186197

187198
.. _geospatial-spherical-geometry:
188199

@@ -195,18 +206,16 @@ Spatial Representation Systems
195206
------------------------------
196207

197208
MongoDB supports two geometry systems for calculating distances
198-
between points. The default is flat geometry and assumes coordinates
199-
points are on a flat surface, which is sufficient for many
200-
applications. To calculate distance using spherical geometry, such as
201-
points that refer to locations on a spherical surface,
202-
(i.e. coordinates on Earth) MongoDB's spherical query operatorss will
203-
provide results using this geometrical system.
209+
between points. The default calculation is based on flat geometry,
210+
which points lie on a flat surface. User spherical query operators to
211+
calculate distances between points that lie on a spherical surface
212+
(i.e. coordinates on Earth.)
204213

205214
.. note::
206215

207216
There is no difference between flat and spherical *data* as stored
208-
in MongoDB. Rather, the only difference is between the distance
209-
calculation in spherical and flat geometry systems.
217+
in MongoDB. Rather, the only difference is the distance
218+
formulas to calculate in flat and spherical geometry.
210219

211220
For more information on query operations for spherical geometry see the
212221
:ref:`spherical queries <geospatial-query-spherical>` section.
@@ -217,7 +226,8 @@ Geohash
217226
-------
218227

219228
To create a geospatial index, MongoDB computes the :term:`geohash`
220-
value for the coordinate pair in the specified values.
229+
value for coordinate pairs within the specified :term:`range
230+
<geospatial-indexes-range>`.
221231

222232
To calculate a geohash value, continuously divide a 2D map into
223233
quadrants. Then, assign each quadrant a two bit value. For example, a
@@ -230,18 +240,18 @@ two bit representation of four quadrants would be:
230240
00 10
231241

232242
These two bit values, ``00``, ``01``, ``10``, and ``11``, represent
233-
each of the quadrants, and points within that quadrant. For a
234-
:term:`geohash` with two bits of resolution, a point in the bottom
243+
each of the quadrants, and all points within that quadrant. For a
244+
:term:`geohash` with two bits of resolution, all points in the bottom
235245
left quadrant would have a geohash of ``00``. The top left quadrant
236246
would have the geohash of ``01``. The bottom right and top right would
237247
have a geohash of ``10`` and ``11``, respectively.
238248

239249
To provide additional precision, continue dividing each quadrant into
240-
sub-quadrants. To identify quadrants within a sub-quadrant, take the
241-
geohash for the containing quadrant (e.g. ``01``) and concatenate the geohash for
242-
the sub-quadrant. Therefore, the geohash for
243-
the upper-right quadrant is ``11``, the geohash for the sub-quadrants would be:
244-
``1101``, ``1111``, ``1110``, and ``1100``.
250+
sub-quadrants. Each sub-quadrant would have the geohash value of the
251+
containing quadrant concatenated with the value of the
252+
sub-quadrant. The geohash for the upper-right quadrant is ``11``, and
253+
the geohash for the sub-quadrants would be: (clockwise from the top
254+
left) ``1101``, ``1111``, ``1110``, and ``1100``, respectfully.
245255

246256
To calculate a more precise :term:`geohash`, continue dividing the
247257
sub-quadrant, concatenate the two-bit identifier for each division. The

0 commit comments

Comments
 (0)