@@ -9,14 +9,16 @@ Overview
9
9
10
10
MongoDB supports location-based queries and geospatial data with a
11
11
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.
15
15
Additionally, geospatial haystack indexes provide additional
16
- support for certain classes of region-based queries .
16
+ support for tuning indexes .
17
17
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,
20
22
:doc:`/applications/geospatial-indexes` provide complete documentation
21
23
of all location-based operations and queries.
22
24
@@ -30,59 +32,60 @@ Geospatial Indexes
30
32
.. see:: :ref:`geospatial-coordinates` for an overview on modeling
31
33
location data in MongoDB.
32
34
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:
37
39
38
40
.. code-block:: javascript
39
41
40
42
db.collection.ensureIndex( { <location field> : "2d" } )
41
43
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.
47
49
48
50
To perform queries on your geospatial data, please see
49
51
:doc:`/applications/geospatial-indexes`.
50
52
51
53
.. note::
52
54
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
56
57
collection will produce unexpected results.
57
58
58
59
.. _geospatial-indexes-range:
59
60
60
61
Range
61
62
~~~~~
62
63
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)``.)
68
70
69
71
To configure the range of a geospatial index, use the ``min`` and
70
72
``max`` options with the :func:`ensureIndex() <db.collection.ensureIndex()>`
71
- operation, as in the following prototype.
73
+ operation, as in the following prototype:
72
74
73
75
.. code-block:: javascript
74
76
75
77
db.collection.ensureIndex( { <location field>: "2d" } ,
76
78
{ min: <lower bound> , max: <upper bound> } )
77
79
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``:
81
84
82
85
.. code-block:: javascript
83
86
84
87
db.places.ensureIndex( { loc: "2d" } ,
85
- { min: 90 , max: 90 } )
88
+ { min: - 90 , max: 90 } )
86
89
87
90
For more information see the :ref:`geospatial precision
88
91
<geospatial-indexes-precision>` and :ref:`geohash
@@ -93,57 +96,63 @@ For more information see the :ref:`geospatial precision
93
96
Precision
94
97
~~~~~~~~~
95
98
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
99
104
relationship between bits and precision, see the :ref:`geohash
100
105
<geospatial-geohash>` section.
101
106
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.
107
111
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
109
113
specifying the ``bits`` option to the :func:`ensureIndex()
110
114
<db.command.ensureIndex()>` method, as in the following prototype:
111
115
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
+
112
120
.. code-block:: javascript
113
121
114
122
db.collection.ensureIndex( {<location field>: "2d"} ,
115
123
{ bits: <bit precision> } )
116
124
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
119
127
precision for query speed.
120
128
121
129
Compound Indexes
122
130
~~~~~~~~~~~~~~~~
123
131
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.
128
134
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:
131
137
132
138
.. code-block:: javascript
133
139
134
140
db.collection.ensureIndex( { <location field>: "2d", <field>: 1 } );
135
141
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.
141
144
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
143
149
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".
147
156
148
157
.. see also: ":ref:`<index-type-compound>`" and ":ref:`<geospatial-haystack-index>`"
149
158
@@ -152,37 +161,39 @@ filter restaurants by type, such as "take-out," or
152
161
Haystack Index
153
162
~~~~~~~~~~~~~~
154
163
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.
159
168
160
169
.. note::
161
170
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``.
165
175
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:
171
178
172
179
.. code-block:: javascript
173
180
174
181
db.collection.ensureIndex({ <location field>: "geoHaystack", type: 1 },
175
182
{ bucketSize: <bucket value> })
176
183
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.
179
187
180
- To query geohaystack indexes, you *must* use the :dbcommand:`geoSearch`
188
+ Geohaystack indexes are only queried by the :dbcommand:`geoSearch`
181
189
command, please see the :ref:`Querying Haystack Indexes
182
- <geospatial-haystack-queries>` section.
190
+ <geospatial-haystack-queries>` section for command details .
183
191
184
192
: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.
186
197
187
198
.. _geospatial-spherical-geometry:
188
199
@@ -195,18 +206,16 @@ Spatial Representation Systems
195
206
------------------------------
196
207
197
208
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.)
204
213
205
214
.. note::
206
215
207
216
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.
210
219
211
220
For more information on query operations for spherical geometry see the
212
221
:ref:`spherical queries <geospatial-query-spherical>` section.
@@ -217,7 +226,8 @@ Geohash
217
226
-------
218
227
219
228
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>`.
221
231
222
232
To calculate a geohash value, continuously divide a 2D map into
223
233
quadrants. Then, assign each quadrant a two bit value. For example, a
@@ -230,18 +240,18 @@ two bit representation of four quadrants would be:
230
240
00 10
231
241
232
242
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
235
245
left quadrant would have a geohash of ``00``. The top left quadrant
236
246
would have the geohash of ``01``. The bottom right and top right would
237
247
have a geohash of ``10`` and ``11``, respectively.
238
248
239
249
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 .
245
255
246
256
To calculate a more precise :term:`geohash`, continue dividing the
247
257
sub-quadrant, concatenate the two-bit identifier for each division. The
0 commit comments