@@ -4,24 +4,28 @@ Search Geospatially
4
4
5
5
.. default-domain:: mongodb
6
6
7
- You can query data based on geographical location using
8
- :manual:`geospatial query operators </geospatial-queries/index.html>`.
9
- Geospatial queries require a :manual:`geospatial index
10
- </geospatial-queries/#geospatial-indexes>`.
11
-
12
7
Overview
13
8
--------
14
9
15
- Geospatial queries work with two different formats depending on your use
16
- case.
10
+ You can query data based on geographical location using geospatial query
11
+ operators. Geospatial queries can be formatted using one of the following
12
+ coordinate systems:
13
+
14
+ - :ref:`Coordinates on an Earth-like Sphere <sphere>`
15
+ - :ref:`Coordinates on a 2D Plane <plane>`
16
+
17
+ This section contains examples of geospatial queries using different
18
+ query operators that you can run against your Atlas sample dataset.
19
+
20
+ .. _sphere:
17
21
18
22
Coordinates on an Earth-like Sphere
19
23
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20
24
21
25
For geospatial queries using longitude and latitude coordinates
22
26
on an Earth-like sphere, use the :manual:`GeoJSON
23
27
</geospatial-queries/index.html#geospatial-geojson>`
24
- format. While GeoJSON has :manual:`multiple types
28
+ query format. While GeoJSON has :manual:`multiple types
25
29
</reference/geojson/>`, all GeoJSON data
26
30
types use some form of the following structure:
27
31
@@ -30,49 +34,66 @@ types use some form of the following structure:
30
34
<field> : {
31
35
type: <GeoJSON type>,
32
36
coordinates: [
33
- [longitude1, latitude1 ],
37
+ [longitude_1, latitude_1 ],
34
38
...
35
- [longitudeN, latitudeN ]
39
+ [longitude_n, latitude_n ]
36
40
]
37
41
}
38
42
39
43
The object type determines the number of coordinates. For instance, a
40
- Point requires only one coordinate: a longitude and a latitude. A Line
41
- uses two coordinates: a longitude and a latitude for each end.
42
- Polygons consist of a list of coordinates in which the first and last
44
+ `` Point`` requires only one coordinate: a longitude and a latitude.
45
+ A ``Line`` uses two coordinates: a longitude and a latitude for each end.
46
+ A ``Polygon`` consists of a list of coordinates in which the first and last
43
47
coordinate are the same, effectively closing the polygon. To learn more
44
48
about the GeoJSON shapes you can use in MongoDB, consult the
45
- :manual:`GeoJSON manual entry </reference/geojson/>`. To geospatially
46
- query GeoJSON data, you must first add your GeoJSON data to a
47
- ``2dsphere`` index. You can create a ``2dsphere`` index by passing the
48
- value ``2dsphere`` for a field in the ``createIndex()`` method.
49
+ :manual:`GeoJSON manual entry </reference/geojson/>`.
50
+
51
+ To enable querying GeoJSON data, you must add the field to a ``2dsphere``
52
+ index. The following snippet creates an index on the ``location.geo`` field in
53
+ the ``movies`` collection using the ``createIndex()`` method:
54
+
55
+ .. code-block:: javascript
56
+
57
+ db.movies.createIndex({location.geo: "2dsphere"})
58
+
59
+ .. _plane:
49
60
50
61
Coordinates on a 2D Plane
51
62
~~~~~~~~~~~~~~~~~~~~~~~~~
52
63
53
- Geospatial queries can also use x and y coordinates in a two dimensional
54
- Euclidean plane. To express data in this way, use the
55
- :manual:`legacy coordinate pair
56
- </geospatial-queries/index.html# legacy- coordinate- pairs>`
57
- format. Legacy coordinate pairs use some form of the following
58
- structure:
64
+ You can also express geospatial queries using ``x`` and ``y`` coordinates in
65
+ a two-dimentional Euclidian plane. Until MongoDB, this was the only format
66
+ compatible with geospatial queries, and are now referred to as
67
+ " legacy coordinate pairs".
68
+
69
+ Legacy coordinate pairs use the following structure:
59
70
60
71
.. code-block:: javascript
61
72
62
73
<field> : [ x, y ]
63
74
64
- The indexed field should contain an array in which the first value
65
- represents an ``x`` axis value and the second value represents a ``y``
66
- coordinate value. You can create a ``2d`` index by passing the value
67
- ``2d`` for a field in the ``createIndex()`` method.
75
+ The field should contain an array of two values in which the first represents
76
+ the ``x`` axis value and the second represents the ``y`` axis value.
77
+
78
+ To enable querying using legacy coordinate pairs, create a ``2d`` index on
79
+ the field on the collection. The following snippet creats an index on the
80
+ ``coordinates`` field in the ``shipwrecks`` collection using the
81
+ ``createIndex()`` method:
82
+
83
+ .. code-block:: javascript
84
+
85
+ db.shipwrecks({coordinates: "2d"})
86
+
87
+ See the
88
+ :manual:`MongoDB server manual page on legacy coordinate pairs </geospatial-queries/index.html#legacy-coordinate-pairs>`
89
+ for more information.
68
90
69
91
.. note::
70
92
71
93
Spherical (``2dsphere``) and flat (``2d``) indexes support some, but
72
94
not all, of the same query operators. For a full list of operators
73
95
and their index compatibility, consult the
74
- :manual:`manual entry for geospatial queries
75
- </geospatial-queries/index.html#geospatial-models>`.
96
+ :manual:`manual entry for geospatial queries </geospatial-queries/index.html#geospatial-models>`.
76
97
77
98
Examples
78
99
--------
@@ -81,17 +102,6 @@ The following examples use the ``theaters`` collection of the
81
102
``sample_mflix`` sample database available in MongoDB Atlas, with a
82
103
``2dsphere`` index on the ``location.geo`` field.
83
104
84
- .. note::
85
-
86
- Geo searches require a geospatial index. To create a ``2dsphere``
87
- index on the ``location.geo`` field of the ``theaters`` collection of
88
- the ``sample_mflix`` database, use the following command on the
89
- sample_mflix database:
90
-
91
- .. code-block:: javascript
92
-
93
- db.movies.createIndex({location.geo: "2dsphere"})
94
-
95
105
Query by Proximity
96
106
~~~~~~~~~~~~~~~~~~
97
107
@@ -122,3 +132,6 @@ England area:
122
132
:start-after: start range geo example
123
133
:end-before: end range geo example
124
134
:dedent: 4
135
+
136
+ See the :manual:`MongoDB server manual page on geospatial query operators </geospatial-queries/index.html>`
137
+ for more information on the operators you can use in your query.
0 commit comments