@@ -4,24 +4,28 @@ Search Geospatially
44
55.. default-domain:: mongodb
66
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-
127Overview
138--------
149
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:
1721
1822Coordinates on an Earth-like Sphere
1923~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2024
2125For geospatial queries using longitude and latitude coordinates
2226on an Earth-like sphere, use the :manual:`GeoJSON
2327</geospatial-queries/index.html#geospatial-geojson>`
24- format. While GeoJSON has :manual:`multiple types
28+ query format. While GeoJSON has :manual:`multiple types
2529</reference/geojson/>`, all GeoJSON data
2630types use some form of the following structure:
2731
@@ -30,49 +34,66 @@ types use some form of the following structure:
3034 <field> : {
3135 type: <GeoJSON type>,
3236 coordinates: [
33- [longitude1, latitude1 ],
37+ [longitude_1, latitude_1 ],
3438 ...
35- [longitudeN, latitudeN ]
39+ [longitude_n, latitude_n ]
3640 ]
3741 }
3842
3943The 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
4347coordinate are the same, effectively closing the polygon. To learn more
4448about 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:
4960
5061Coordinates on a 2D Plane
5162~~~~~~~~~~~~~~~~~~~~~~~~~
5263
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:
5970
6071.. code-block:: javascript
6172
6273 <field> : [ x, y ]
6374
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.
6890
6991.. note::
7092
7193 Spherical (``2dsphere``) and flat (``2d``) indexes support some, but
7294 not all, of the same query operators. For a full list of operators
7395 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>`.
7697
7798Examples
7899--------
@@ -81,17 +102,6 @@ The following examples use the ``theaters`` collection of the
81102``sample_mflix`` sample database available in MongoDB Atlas, with a
82103``2dsphere`` index on the ``location.geo`` field.
83104
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-
95105Query by Proximity
96106~~~~~~~~~~~~~~~~~~
97107
@@ -122,3 +132,6 @@ England area:
122132 :start-after: start range geo example
123133 :end-before: end range geo example
124134 :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