1- =====================
2- Query Geospatial Data
3- =====================
1+ ======================================
2+ Geospatial Queries with ``2d`` Indexes
3+ ======================================
44
55.. default-domain:: mongodb
66
7- Geospatial data stores location values for documents that can be used to
8- query documents based on geographic location, such as city, region, or
9- address. For an introduction to geospatial data, see
10- :doc:`/core/geospatial-indexes`.
7+ MongoDB provides support for querying location-based data using
8+ special geospatial indexes. For an introduction to these ``2d``
9+ indexes, see :doc:`/core/geospatial-indexes`.
1110
12- You can query geospatial data using the following approaches :
11+ MongoDB supports the following geospatial query types :
1312
14- - Proximity queries, which query based on distance to a given point. See
15- :ref:`geospatial-indexes-proximity`.
13+ - Proximity queries, which select documents based on distance to a
14+ given point. See :ref:`geospatial-indexes-proximity`.
1615
17- - Bounded queries, which return documents within a defined area. Bounded
18- queries do not sort results and are faster than proximity queries. See
19- :ref:`geospatial-indexes-bounded`.
16+ - Bounded queries, which select documents that have coordinates within
17+ a specified area. See :ref:`geospatial-indexes-bounded`.
2018
21- - Query for exact matches using the :method:`find()
22- <db.collection.find()>`. Exact geospatial queries have applicability
23- for a limited selection of cases. See
24- :ref:`geospatial-indexes-exact-match`.
19+ - Exact queries, which select documents with an exact coordinate pair,
20+ which has limited applicability. See :ref:`geospatial-indexes-exact-match`.
2521
2622.. index:: geospatial queries; proximity
2723.. _geospatial-indexes-near:
2824.. _geospatial-indexes-proximity:
2925
3026Proximity Queries
31- -----------------
27+ ~~~~~~~~~~~~~~~~~
3228
33- Proximity queries find the first "N" documents closest to a point. To
34- perform proximity queries you use either the :method:`find()
35- <db.collection.find()>` method with the :operator:`$near` operator or
36- you use the :dbcommand:`geoNear` command.
29+ Proximity queries select the documents closest to the point specified
30+ in the query. To perform proximity queries you use either the
31+ :method:`find() <db.collection.find()>` method with the
32+ :operator:`$near` operator or you use the :dbcommand:`geoNear`
33+ command.
3734
3835The :method:`find() <db.collection.find()>` method with the
3936:operator:`$near` operator returns 100 documents by default and sorts
@@ -58,7 +55,7 @@ following form:
5855
5956 { "_id" : ObjectId(" ... "), "loc" : [ -73, 40 ] }
6057
61- The :dbcommand:`geoNear` command provides more information than does the
58+ The :dbcommand:`geoNear` command returns more information than does the
6259:operator:`$near` operator but does not sort results. The
6360:dbcommand:`geoNear` command also offers additional operators, such as
6461operators to query for :ref:`maximum <geospatial-indexes-distance>` or
@@ -72,7 +69,7 @@ following form:
7269
7370 db.runCommand( {geoNear: "[collection]", near: [ x, y ] } )
7471
75- .. example::
72+ .. example::
7673
7774 The following command returns the same results as the
7875 :operator:`near` in the previous example but with more information:
@@ -81,7 +78,7 @@ following form:
8178
8279 db.runCommand( {geoNear: "places", near: [ -74, 40.74 ] } )
8380
84- The output is :
81+ This operation will return the following output :
8582
8683 .. code-block:: javascript
8784
@@ -141,16 +138,17 @@ To specify distance with the :dbcommand:`geoNear` command, use the
141138Limit the Number of Results
142139~~~~~~~~~~~~~~~~~~~~~~~~~~~
143140
144- By default, geospatial queries with the :method:`find()
145- <db.collection.find()>` method return 100 documents, sorted by distance.
141+ By default, geospatial queries using :method:`find()
142+ <db.collection.find()>` method return 100 documents, sorted by
143+ distance.
146144
147145To limit the result when using the :method:`find()
148146<db.collection.find()>` method, use the :method:`limit()
149- <cursor.limit()>` method. The following is the prototype operation :
147+ <cursor.limit()>` method, as in the following prototype :
150148
151149.. code-block:: javascript
152150
153- db.collection.find( { <location field>: { $near: [ x, y ] } } ).limit(n )
151+ db.collection.find( { <location field>: { $near: [ x, y ] } } ).limit(<n> )
154152
155153To limit the result set when using the :dbcommand:`geoNear` command, use
156154the ``num`` option. The following is a prototype of the command:
@@ -159,47 +157,43 @@ the ``num`` option. The following is a prototype of the command:
159157
160158 db.runCommand( { geoNear: "collection", near: [ x, y ], num: z } )
161159
162- The :method:`limit() <cursor.limit()>` method and ``near`` parameter do
163- not limit geospatial query results by distance, only the number of
164- results. To limit geospatial search results by distance, please see
165- the :ref:`geospatial-indexes-distance` section.
160+ The :method:`limit() <cursor.limit()>` method and ``near`` parameter
161+ to :dbcommand:`geoNear` do not limit geospatial query results by
162+ distance, only the number of results. To limit geospatial search
163+ results by distance, please see the :ref:`geospatial-indexes-distance`
164+ section.
166165
167166.. note::
168167
169168 The :method:`limit() <cursor.limit()>` method and ``num`` option have
170169 different performance characteristics. Geospatial queries using
171170 :method:`limit() <cursor.limit()>` method are slower than using
172- :dbcommand:`geoNear`.
171+ :dbcommand:`geoNear`.
173172
174173 Geospatial queries with the :method:`find() <db.collection.find()>`
175174 method will return 100 documents, sort them, and finally limit the
176175 result set. Geospatial queries with the :dbcommand:`geoNear` and
177176 ``num`` option will only return the specified number of unsorted
178177 documents.
179178
180- .. TODO double check with greg
181-
182179.. _geospatial-indexes-within:
183180.. _geospatial-indexes-bounded:
184181
185182Bounded Queries
186183---------------
187184
188- Bounded queries return documents within a particular shape and use the
189- :operator:`$within` operator to define the shape.
190-
191- Bounded queries can use the following shapes:
185+ Bounded queries return documents within a shape defined using the
186+ :operator:`$within` operator. MongoDB's bounded queries support the
187+ following shapes:
192188
193189- :ref:`geospatial-indexes-circles`
194190- :ref:`geospatial-indexes-rectangles`
195191- :ref:`geospatial-indexes-polygons`
196192
197- Bounded queries do not return sorted results and are faster than
198- :ref:`geospatial-indexes-proximity`.
199-
200- .. TODO update $near in operator pages
201-
202- Bounded queries take the following prototype form:
193+ Bounded queries do not return sorted results. As a result MongoDB can
194+ return bounded queries more quickly than :ref:`proximity queries
195+ <geospatial-indexes-proximity>`. Bounded queries have the following
196+ form:
203197
204198.. code-block:: javascript
205199
@@ -209,16 +203,18 @@ Bounded queries take the following prototype form:
209203 }
210204 } )
211205
212- The following sections describe how to query based on different shapes.
206+ The following sections describe each of the shapes supported by
207+ bounded queries:
213208
214209.. _geospatial-indexes-circles:
215210
216211Circles
217212~~~~~~~
218213
219- To query for documents within a circle, you specify the center and
220- the radius of the circle using the :operator:`$within` operator and
221- :operator:`$center` option. Consider the following prototype query:
214+ To query for documents with coordinates inside the bounds of a circle,
215+ specify the center and the radius of the circle using the
216+ :operator:`$within` operator and :operator:`$center` option. Consider
217+ the following prototype query:
222218
223219.. code-block:: javascript
224220
@@ -235,21 +231,21 @@ radius of ``10``, using a geospatial index on the ``loc`` field:
235231 }
236232 } )
237233
238- The :operator:`$within` operator using :operator:`$center` is similar to
239- using :operator:`$maxDistance`, but :operator:`$center` has different
240- performance characteristics. Queries using the :operator:`$within`
241- operator are not sorted, while :operator:`$near ` operator results are
242- sorted .
234+ The :operator:`$within` operator using :operator:`$center` is similar
235+ to using :operator:`$maxDistance`, but :operator:`$center` has
236+ different performance characteristics. MongoDB does not sort queries
237+ that use the :operator:`$within ` operator are not sorted, unlike
238+ queries using the :operator:`$near` operator .
243239
244240.. _geospatial-indexes-rectangles:
245241
246242Rectangles
247243~~~~~~~~~~
248244
249- To query for documents that lie within a rectangle, you specify the
250- lower-left and upper-right corners of the rectangle using the
251- :operator:`$within` operator and :operator:`$box` option. Consider the
252- following prototype query:
245+ To query for documents with coordinates inside the bounds of a
246+ rectangle, specify the lower-left and upper-right corners of the
247+ rectangle using the :operator:`$within` operator and :operator:`$box`
248+ option. Consider the following prototype query:
253249
254250.. code-block:: javascript
255251
@@ -270,10 +266,9 @@ Polygons
270266~~~~~~~~
271267
272268.. versionadded:: 1.9
273-
274269 Support for polygon queries.
275270
276- To query for documents that lie within a polygon, you specify the points
271+ To query for documents with coordinates inside of a polygon, specify the points
277272of the polygon in an array, using the the :operator:`$within` operator
278273with the :operator:`$polygon` option. MongoDB automatically connects the
279274last point in the array to the first point. Consider the following
@@ -284,12 +279,12 @@ prototype query:
284279 db.places.find({ "loc": { "$within": { "$polygon": [ points ] } } })
285280
286281The following query returns all documents that have coordinates
287- that exist within ``[0,0]``, `` [3,3]`` , `` [6,0]``:
282+ that exist within the polygon defined by ``[ [ 0,0], [3,3], [6,0] ]``:
288283
289284.. code-block:: javascript
290285
291286 db.places.find({ "loc": { "$within": { "$polygon":
292- [0,0], [3,3], [6,0] } } } )
287+ [ [ 0,0], [3,3], [6,0] ] } } } )
293288
294289.. index:: geospatial queries
295290.. index:: geospatial queries; exact
@@ -298,33 +293,34 @@ that exist within ``[0,0]``, ``[3,3]`` , ``[6,0]``:
298293Query for Exact Matches
299294-----------------------
300295
301- You can use the :method:`find() < db.collection.find()> ` method to query
302- for an exact match on a location. These queries have the following form:
296+ You can use the :method:`db.collection.find()` method to query for an
297+ exact match on a location. These queries have the following form:
303298
304299.. code-block:: javascript
305300
306301 db.collection.find( { <location field>: [ x, y ] } )
307302
308303This query will return any documents with the value of ``[ x, y ]``.
309304
310- Exact geospatial queries have applicability for a limited selection of
311- cases, the :ref:`proximity <geospatial-indexes-proximity>` method and
312- :ref:`bounded <geospatial-indexes-bounded>` method provide more useful
313- results.
305+ Exact geospatial queries only applicability for a limited selection of
306+ cases, the :ref:`proximity <geospatial-indexes-proximity>` and
307+ :ref:`bounded <geospatial-indexes-bounded>` queries provide more useful
308+ results for more applications .
314309
315310.. _geospatial-indexes-spherical:
316311
317312Calculate Distances Using Spherical Geometry
318313--------------------------------------------
319314
320- When you query location data , MongoDB by default calculates distances using flat
321- geometry, which models points on a flat surface.
315+ When you query using the ``2d`` index , MongoDB calculates distances
316+ using flat geometry by default , which models points on a flat surface.
322317
323- Optionally, you can calculate distances using spherical geometry, which
324- models points on a spherical surface (i.e. coordinates on Earth).
318+ Optionally, you may instruct MongoDB to calculate distances using spherical geometry, which
319+ models points on a spherical surface. Spherical geometry is useful for
320+ modeling coordinates on the surface of Earth.
325321
326322To calculate distances using spherical geometry, use MongoDB's
327- spherical operators or options. You can use the :
323+ spherical query operators and options:
328324
329325- :method:`find() <db.collection.find()>` method with the
330326 :operator:`$nearSphere` operator.
@@ -344,8 +340,10 @@ calculation, see :ref:`geospatial-indexes-distance-calculation`.
344340Distance Multiplier
345341-------------------
346342
347- The ``distanceMultiplier`` option multiplies all distances returned by
348- :dbcommand:`geoNear` command by an assigned value.
343+ The ``distanceMultiplier`` option :dbcommand:`geoNear` returns
344+ distances only after multiplying the results by command by an assigned
345+ value. This allows MongoDB to return converted values, and removes the
346+ requirement to convert units in application logic.
349347
350348.. note::
351349
@@ -356,8 +354,8 @@ The ``distanceMultiplier`` option multiplies all distances returned by
356354
357355Using ``distanceMultiplier`` in spherical queries allows one to use
358356results from the :dbcommand:`geoNear` command without radian to
359- distance conversion. The following example uses ``distanceMultiplier`` in the
360- :dbcommand:`geoNear` command with a :ref:`spherical
357+ distance conversion. The following example uses ``distanceMultiplier``
358+ in the :dbcommand:`geoNear` command with a :ref:`spherical
361359<geospatial-indexes-spherical>` example:
362360
363361.. code-block:: javascript
@@ -368,7 +366,7 @@ distance conversion. The following example uses ``distanceMultiplier`` in the
368366 distanceMultiplier: 3963.192
369367 } )
370368
371- The output of the above command would be :
369+ The output of the above operation would resemble the following :
372370
373371.. code-block:: javascript
374372
@@ -394,16 +392,15 @@ The output of the above command would be:
394392 "ok" : 1
395393 }
396394
397- .. seealso::
398- :ref:`Distance operator <geospatial-indexes-distance>`
395+ .. seealso:: :ref:`Distance operator <geospatial-indexes-distance>`
399396
400397.. _geospatial-indexes-haystack-queries:
401398
402399Querying Haystack Indexes
403400-------------------------
404401
405- Haystack indexes are a special geospatial index that are optimized for a
406- small area . To create geospatial indexes see
402+ Haystack indexes are a special ``2d`` geospatial index that optimized
403+ to return results over small areas . To create geospatial indexes see
407404:ref:`geospatial-indexes-haystack-index`.
408405
409406To query the haystack index, use the :dbcommand:`geoSearch`
@@ -412,7 +409,7 @@ command. You must specify both the coordinate and other field to
412409
413410.. code-block:: javascript
414411
415- db.runCommand( { geoSearch: <haystack index>,
412+ db.runCommand( { geoSearch: <haystack index>,
416413 search: { <field>: <value> } } )
417414
418415For example, to return all documents with the value ``restaurants`` in
@@ -426,14 +423,14 @@ the ``type`` field near the example point, the command would resemble:
426423
427424.. note::
428425
429- Haystack indexes are not suited to returning the closest documents to
430- a particular location, as the closest documents could be far away
431- compared to the ``bucketSize``.
426+ Haystack indexes are not suited to returning a full list of the
427+ closest documents to a particular location, as the closest
428+ documents could be far away compared to the ``bucketSize``.
432429
433430.. note::
434431
435- :ref:`Spherical queries <geospatial-indexes-spherical>` are
436- not currently supported by haystack indexes.
432+ :ref:`Spherical query operations <geospatial-indexes-spherical>`
433+ are not currently supported by haystack indexes.
437434
438435 The :method:`find() <db.collection.find()>` method and
439436 :dbcommand:`geoNear` command cannot access the haystack index.
0 commit comments