@@ -4,12 +4,12 @@ Using Geospatial Data
4
4
5
5
.. default-domain:: mongodb
6
6
7
- MongoDB provides rich location aware queries that return documents
8
- based on location with a special geospatial index type. This document
9
- introduces geospatial data modeling, indexing operations, and provides
10
- example queries using the :ref:`geospatial query operators
11
- <geospatial-query-operators>`. For more information about
12
- geospatial indexes and operations see the :doc:`/core/geospatial-indexes` document.
7
+ MongoDB provides functionality to store and query geospatial data with
8
+ specialized operators. This document introduces geospatial data
9
+ modeling, indexing operations, and provides example queries using the
10
+ :ref:`geospatial query operators <geospatial-query-operators>`. For
11
+ more information about geospatial indexes and operations see the
12
+ :doc:`/core/geospatial-indexes` document.
13
13
14
14
.. _geospatial-coordinates:
15
15
@@ -24,22 +24,27 @@ geospatial indexes and operations see the :doc:`/core/geospatial-indexes` docume
24
24
Queries
25
25
-------
26
26
27
- MongoDB provides special :ref:`geospatial query operators
28
- <geospatial-query-operators>` for performing queries on location data
29
- inside the normal :func:`find() <db.collection.find()>` method. The
30
- :dbcommand:`geoNear` command also returns results using geospatial
31
- indexes, but also includes additional geospatial information in the
32
- return documents.
27
+ There are two operators to query geospatial data in MongoDB,
28
+ the general :func:`find() <db.collection.find()>` method and the
29
+ specialized :dbcommand:`geoNear` command.
30
+
31
+ .. TODO find a better way to say this... (rewrite 3x)
32
+ The :func:`find() <db.collection.find()>` method for geospatial data
33
+ is same as querying any other data. This provides simplicity.
34
+
35
+ The :dbcommand:`geoNear` command is more specialized as it returns
36
+ detailed geospatial information such as distances, ... This provides
37
+ addtional benefit when only working with geospatial data.
38
+
39
+ .. TODO does it make sense to have this here??
33
40
34
41
.. note::
35
42
36
- By default MongoDB assumes calculate distances using flat geometry
37
- and all distances calculated by the :dbcommand:`geoNear` use the
38
- Pythagorean distance formula.
43
+ By default, MongoDB calculates distances using flat geometry.
39
44
40
45
MongoDB can also calculate distances based on :ref:`spherical
41
- geometry <geospatial-spherical-representation>`, using
42
- :ref:`spherical query operations <geospatial-spherical-queries>`.
46
+ geometry <geospatial-spherical-representation>` by using the
47
+ :ref:`spherical query operators <geospatial-spherical-queries>`.
43
48
44
49
.. index:: geospatial queries; exact
45
50
@@ -49,25 +54,18 @@ Exact
49
54
~~~~~
50
55
51
56
You can use the :func:`find() <db.collection.find()>` method to query
52
- for an exact match on a location. These queries take the
53
- following prototypical form:
57
+ for an exact match on a location. These queries have the prototypical
58
+ form:
54
59
55
60
.. code-block:: javascript
56
61
57
62
db.collection.find( { <location field>: [ x, y ] } )
58
63
59
- This query will return any document that where the value of ``[ x, y
60
- ]`` is *exactly* the same as the one specified in the query. To return
61
- all documents in the ``places`` collection with values in the ``loc``
62
- field that are exactly ``[ -74, 40.74 ]``, consider the following example:
64
+ This query will return any documents with the value of ``[ x, y ]``.
63
65
64
- .. code-block:: javascript
65
-
66
- db.places.find( { "loc": [ -74, 40.74 ] } )
67
-
68
- Exact geospatial queries only have applicability for a limited selection of
69
- cases, :ref:`proximity <geospatial-query-proximity>` and :ref:`bounded
70
- <geospatial-query-bounded>` provide more useful results.
66
+ Exact geospatial queries have applicability for a limited selection of
67
+ cases, the :ref:`proximity <geospatial-query-proximity>` method and :ref:`bounded
68
+ <geospatial-query-bounded>` method provide more useful results.
71
69
72
70
.. index:: geospatial queries; proximity
73
71
.. _geospatial-query-near:
@@ -76,32 +74,29 @@ cases, :ref:`proximity <geospatial-query-proximity>` and :ref:`bounded
76
74
Proximity
77
75
~~~~~~~~~
78
76
79
- Proximity queries take a coordinate pair and return a result set of
80
- documents from the geospatial index that are close to the query
81
- coordinates, sorted by distance. You can use the :operator:`$near`
82
- operator in a :func:`find() <db.collection.find()>` query to return
83
- the 100 closest points to a coordinate (e.g. ``[ x, y ]``.) These
84
- queries have the following prototype form :
77
+ To find all documents that are within a proximity of a point, use the
78
+ :operator:`$near` operator with the :func:`find()
79
+ <db.collection.find()>` method. By default, the :operator:`$near` will
80
+ return 100 points sorted by distance.
81
+
82
+ The prototype form for the :operator:`$near` operator is :
85
83
86
84
.. code-block:: javascript
87
85
88
86
db.collection.find( { <location field>: { $near: [ x, y ] } } )
89
87
90
- Consider the following example query:
91
-
92
- .. code-block:: javascript
88
+ .. TODO show example & output (i.e. show that it's the same as normal ops)
93
89
94
- db.places.find( { loc: { $near: [ -74, 40.74 ] } } )
90
+ The :dbcommand:`geoNear` command provides the equivalent functionality
91
+ to the :operator:`$near` operator but does not sort the results,
92
+ returns more information for each document found, and provides
93
+ additional operators.
95
94
96
- This operation will return documents from a collection named
97
- ``places`` that has a geospatial index on the ``loc`` field, near the
98
- coordinates ``[ -74, 40.74 ]``.
95
+ .. TODO what are the additional operators for geoNear that can't be
96
+ .. used by find()??
99
97
100
- In addition to :operator:`near`, the :dbcommand:`geoNear` command
101
- provides equivalent functionality. :dbcommand:`geoNear` adds
102
- additional options and returns more information for each
103
- document found. In its most simple form, the :dbcommand:`geoNear`
104
- command has the following prototypical form:
98
+ In its most simple form, the :dbcommand:`geoNear` command has the
99
+ following prototypical form:
105
100
106
101
.. code-block:: javascript
107
102
@@ -114,6 +109,9 @@ in the previous example:
114
109
115
110
db.runCommand( {geoNear: "places", near: [ -74, 40.74 ] } )
116
111
112
+ .. TODO show output (i.e. highlight differences to $near)
113
+
114
+
117
115
.. seealso::
118
116
119
117
:ref:`geospatial-query-exact`
@@ -123,8 +121,11 @@ in the previous example:
123
121
Limit
124
122
`````
125
123
124
+ The :func:`limit() <cursor.limit()>` method can be used with
125
+ :func:`find() <db.collection.find()>`
126
+
126
127
By default, geospatial queries with :func:`find()
127
- <db.collection.find()>` return 100 documents. To impose a limit
128
+ <db.collection.find()>` return 100 documents sorted by distance. To limit
128
129
on the result set, use the :func:`limit() <cursor.limit()>` method
129
130
with :func:`find() <db.collection.find()>` operator. The following is
130
131
the prototype operation:
@@ -133,13 +134,7 @@ the prototype operation:
133
134
134
135
db.collection.find( { <location field>: { $near: [ x, y ] } } ).limit(n)
135
136
136
- The following example will return only 20 results of the above query:
137
-
138
- .. code-block:: javascript
139
-
140
- db.places.find( { loc: { $near: [ -74, 40.74 ] } } ).limit(20)
141
-
142
- You may also use the ``num`` option with the :dbcommand:`geoNear`
137
+ To the ``num`` option with the :dbcommand:`geoNear`
143
138
command and the ``near`` parameter, as in the following prototype:
144
139
145
140
.. code-block:: javascript
0 commit comments