@@ -15,204 +15,7 @@ the geospatial indexes and its operations see :doc:`Geospatial Indexes
15
15
16
16
.. _geospatial-coordinates:
17
17
18
- Geospatial Coordinates
19
- ----------------------
20
-
21
- For geospatial indexes, you must model your coordinate data in a field
22
- that holds a 2 dimensional array. The preferred form is:
23
-
24
- .. code-block:: javascript
25
-
26
- [ x, y ]
27
-
28
- Consistency is crucial: all documents must store the values in the
29
- same order. you may also use an embeded document, as in:
30
-
31
- .. code-block:: javascript
32
-
33
- { x: 1, y: 2 }
34
-
35
- MongoDB will not read field names in embedded documents, so order is
36
- still important when using this model.
37
-
38
- .. note::
39
-
40
- If you use latitude/longitude data as your coordinate system,
41
- always store latitude values first: MongoDB's :ref:`spherical
42
- queries <geospatial-query-spherical>` only recognize ``[
43
- latitude, longitude ]`` ordering.
44
-
45
- .. _geospatial-indexes:
46
-
47
- Geospatial Indexes
48
- ------------------
49
-
50
- .. see:: :ref:`geospatial-coordinates` for an overview on modeling
51
- location data in MongoDB.
52
-
53
- To create a geospatial index, use an operation modeled on the
54
- following prototype:
55
-
56
- .. code-block:: javascript
57
-
58
- db.collection.ensureIndex( { <location field> : "2d" } )
59
-
60
- These operations will create a special index on location field in the
61
- specified collection. These indexes use :ref:`geospatial-geohash`. All
62
- geospatial queries will use this geospatial index.
63
-
64
- .. note::
65
-
66
- MongoDB only supports *one* geospatial index per collection. As
67
- with all indexes in MongoDB, any single query will only be able to
68
- use one index. If a collection were to have more than one
69
- geospatial index, geospatial queries could not return consistent
70
- results.
71
-
72
- .. _geospatial-indexes-range:
73
-
74
- Range
75
- ~~~~~
76
-
77
- All geospatial indexes are bounded and MongoDB will return an error
78
- and reject documents with coordinate pairs outside of these
79
- boundaries. The default boundaries support global coordinate data
80
- (i.e. latitude and longitude for points on Earth,) are between -180
81
- inclusive, and 180 non-inclusive.
82
-
83
- To specify the boundaries of a geospatial index, use the ``min`` and
84
- ``max`` operators with the :func:`ensureIndex() <db.collection.ensureIndex()>`
85
- operation, as in the following prototype.
86
-
87
- .. code-block:: javascript
88
-
89
- db.collection.ensureIndex( { <location field>: "2d" } ,
90
- { min: <lower bound> , max: <upper bound> } )
91
-
92
- The following operation will create an index on the ``places``
93
- collection, for coordinates in the ``loc`` field, with boundaries
94
- between ``-90`` and ``90``:
95
-
96
- .. code-block:: javascript
97
-
98
- db.places.ensureIndex( { loc: "2d" } ,
99
- { min: 90 , max: 90 } )
100
-
101
- .. _geospatial-indexes-precision:
102
-
103
- Precision
104
- ~~~~~~~~~
105
-
106
- Geospatial indexes record precision, or resolution, in "bits", which
107
- are configurable during index creation. If a geospatial index has a
108
- higher bits setting, MongoDB will be able to return more precise
109
- results from the index. However, lower resolution indexes provide
110
- faster performance. For more information, please refer to the
111
- :ref:`precision <geospatial-indexes-precision>` section.
112
-
113
- By default, geospatial indexes in MongoDB have 26 bits of precision
114
- and supports as many as 32 bits of precision. You can set the
115
- precision of a geospatial index during creation by specifying the
116
- ``bits`` option to the :func:`ensureIndex()
117
- <db.command.ensureIndex()>` method, as in the following example.
118
-
119
- .. code-block:: javascript
120
-
121
- db.collection.ensureIndex( {<location field>: "2d"} ,
122
- { bits: <bit precision> } )
123
-
124
- You may create an index with fewer than 26 bits *if* your the data in
125
- your collection is less precise and/or you're willing to sacrifice
126
- precision in exchange for query speed.
127
-
128
- Compound Indexes
129
- ~~~~~~~~~~~~~~~~
130
-
131
- MongoDB supports :term:`compound indexes <compound index>` where one component is a
132
- coordinate in a geospatial index, and the other coordinate is one or
133
- more fields. This means that, for some operations, MongoDB will be
134
- able to use this index for a larger portion of an operation, which
135
- will improve performance for these queries.
136
-
137
- Use an operation that resembles the following prototype command to
138
- create a compound geospatial index.
139
-
140
- .. code-block:: javascript
141
-
142
- db.collection.ensureIndex( { <location field>: "2d", <field>: 1 } );
143
-
144
- These compound indexes support queries where you must filter both by
145
- location and by another field. For example, if you need to return a
146
- list of restaurants near a given point, but you want to optionally
147
- select only restaurants that match a certain type (i.e. "take-out," or
148
- "bar" stored in a ``type`` field.)
149
-
150
- See the :ref:`index-type-compound` section for more information on
151
- geospatial indexes.
152
-
153
- .. note::
154
-
155
- Limits in geospatial queries are always applied to the geospatial
156
- component first. This will affect your result set if you specify
157
- additional sort operations.
158
-
159
- Haystack Indexing
160
- ~~~~~~~~~~~~~~~~~
161
-
162
- Geospatial haystack indexes makes it possible to build a special ``bucket``
163
- collection that can better support queries that operate within a
164
- limited area. For more information, please refer to :doc:`Geospatial
165
- Indexes </core/geospatial-indexes>`
166
-
167
- Build a geospatial index and specify the ``geoHaystack`` for the
168
- location field and a ``bucketSize`` parameter . The ``bucketSize``
169
- parameter determines the granularity of the bucket index. A
170
- ``bucketSize`` of 1 creates an index where keys within 1 unit of
171
- longitude or latitude are stored in the same bucket.
172
-
173
- .. code-block:: javascript
174
-
175
- db.collection.ensureIndex({ <location field>: "geoHaystack", type: 1 },
176
- { bucketSize: <bucket value> })
177
-
178
- By default, all queries on a geospatial haystack index returns 50
179
- documents.
180
-
181
- For example, to index all restaurants in a particular area with a
182
- given maximum distance of 6 degrees longitude or latitude to accelerate
183
- searches in this area, create the index using the following command:
184
-
185
- .. code-block:: javascript
186
-
187
- db.places.ensureIndex({ loc: "geoHaystack", type: 1} ,
188
- { bucketSize: 6 } )
189
-
190
- .. _geospatial-spherical-representation:
191
-
192
- Spatial Representation Systems
193
- ------------------------------
194
-
195
- MongoDB supports two systems for representing and returning geospatial
196
- results. The default representation is flat and assumes that the
197
- coordinates represent points on a flat plane. While this
198
- representation is sufficient for many applications, if the points
199
- refer to locations on a spherical plane (i.e. coordinates on Earth)
200
- then sphical queries will provide more accurate results.
201
-
202
- .. note::
203
-
204
- There is no difference between flat and spherical *data* as stored
205
- in MongoDB. Rather, the only difference between spherical and flat
206
- geospatial systems in MongoDB is in the **queries**.
207
-
208
- In general, the flat system is easier and accurate for system data
209
- sets, but will return imprecise or skewed results if your coordinate
210
- system reflects points on a curved plane, like the Earth's surface.
211
-
212
- For more information on spherical and flat queries see the
213
- :ref:`geospatial-representation-system` section and for more
214
- information on query operations for spherical systems see the
215
- :ref:`spherical queries <geospatial-query-spherical>` section.
18
+ .. include:: /includes/geospatial-coordinates.rst
216
19
217
20
.. index:: geospatial queries
218
21
.. _geospatial-queries:
@@ -624,12 +427,4 @@ in the following example:
624
427
625
428
db.records.ensureIndex( { "addresses.loc": "2d" } )
626
429
627
- Geospatial Indexes and Sharding
628
- -------------------------------
629
-
630
- You cannot use a geospatial index as a :term:`shard key` to shard a
631
- collection. However, you *can* create and maintain a geospatial index
632
- on a sharded collection, as long as the shard key is another
633
- field. Your application may query for geospatial data using
634
- :dbcommand:`geoNear` and :operator:`$within`; however, queries using
635
- :operator:`$near` are not supported.
430
+ .. includes:: /includes/geospatial-sharding.rst
0 commit comments