Skip to content

Commit b16809c

Browse files
authored
DOCSP-30539: Geospatial search (#88)
1 parent 64703e2 commit b16809c

File tree

3 files changed

+458
-1
lines changed

3 files changed

+458
-1
lines changed

source/fundamentals.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ Fundamentals
2525
/fundamentals/runtimes
2626
/fundamentals/monitoring
2727
/fundamentals/collations
28+
/fundamentals/geo
2829

2930
..
3031
Connect to MongoDB Atlas from AWS Lambda <https://www.mongodb.com/docs/atlas/manage-connections-aws-lambda/>
3132
/fundamentals/transactions
3233
/fundamentals/gridfs
3334
/fundamentals/encrypt-fields
34-
/fundamentals/geo
3535

3636
.. include:: /includes/fundamentals-sections.rst

source/fundamentals/geo.txt

Lines changed: 341 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,341 @@
1+
.. _rust-geo-guide:
2+
3+
===================
4+
Search Geospatially
5+
===================
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: code example, geographic, map, distance
13+
14+
.. contents:: On this page
15+
:local:
16+
:backlinks: none
17+
:depth: 2
18+
:class: singlecol
19+
20+
Overview
21+
--------
22+
23+
In this guide, you can learn how to search **geospatial data** by using the
24+
{+driver-short+}. Geospatial data represents a geographic location on the surface
25+
of the Earth or on a Euclidean plane.
26+
27+
Examples of geospatial data include:
28+
29+
- Locations of movie theaters
30+
- Borders of countries
31+
- Routes of bicycle rides
32+
- Dog exercise areas in New York City
33+
- Points on a graph
34+
35+
This guide includes the following sections:
36+
37+
- :ref:`Store Geospatial Data <rust-store-geo>` describes the data formats you can use to
38+
represent geospatial data
39+
40+
- :ref:`Geospatial Indexes <rust-indexes-geo>` describes how to create an index on fields
41+
storing geospatial data
42+
43+
- :ref:`Geospatial Queries <rust-query-geo>` describes how to query geospatial
44+
data stored in indexed fields
45+
46+
- :ref:`Additional Resources <rust-addtl-info-geo>` provides links to resources and
47+
API documentation for types and methods mentioned in this guide
48+
49+
.. _rust-store-geo:
50+
51+
Store Geospatial Data
52+
---------------------
53+
54+
All geospatial data in MongoDB is stored in one of the following formats:
55+
56+
- GeoJSON, a format that represents geospatial data on an Earth-like
57+
sphere
58+
59+
- Legacy Coordinate Pair, a format that represents geospatial data
60+
on a Euclidean plane
61+
62+
GeoJSON
63+
~~~~~~~
64+
65+
Use GeoJSON to store data that represents geospatial information on
66+
an Earth-like sphere. GeoJSON is composed of one or more **positions**
67+
and a **type**.
68+
69+
Positions
70+
`````````
71+
72+
A position represents a single place on Earth and exists in code as an array
73+
containing the following values:
74+
75+
- Longitude in the first position
76+
- Latitude in the second position
77+
78+
The following code represents the **position** of the MongoDB Headquarters in
79+
New York City, NY:
80+
81+
.. code-block:: rust
82+
:copyable: false
83+
84+
let coords = vec! [-73.986805, 40.7620853];
85+
86+
.. important:: Longitude then Latitude
87+
88+
GeoJSON orders coordinates as **longitude** first and **latitude** second.
89+
This conflicts with geographic coordinate system conventions, which generally list
90+
latitude first and longitude second. Ensure that you reformat your coordinates to
91+
align with GeoJSON standards.
92+
93+
Types
94+
`````
95+
96+
Your GeoJSON object's type determines the geometric shape it represents. Geometric shapes are
97+
made up of positions.
98+
99+
The following list describes common GeoJSON types and how to specify them with positions:
100+
101+
- ``Point``: a single position. For example, the following ``Point`` represents the location of
102+
the MongoDB Headquarters:
103+
104+
.. literalinclude:: /includes/fundamentals/code-snippets/crud/geo.rs
105+
:start-after: start-point
106+
:end-before: end-point
107+
:language: rust
108+
:dedent:
109+
110+
- ``LineString``: an array of two or more positions that forms a series of line
111+
segments. A ``LineString`` can represent a path, route, border, or any other linear
112+
geospatial data. For example, the following ``LineString`` represents a segment of
113+
`the Great Wall of China <https://commons.wikimedia.org/wiki/File:GreatWallChina4.png>`__:
114+
115+
.. literalinclude:: /includes/fundamentals/code-snippets/crud/geo.rs
116+
:start-after: start-linestring
117+
:end-before: end-linestring
118+
:language: rust
119+
:dedent:
120+
121+
- ``Polygon``: an array of positions in which the first and last
122+
position are the same and enclose some space. For example, the following
123+
``Polygon`` represents `the land within Vatican City
124+
<https://commons.wikimedia.org/wiki/File:Vatican_City_map_EN.png>`__:
125+
126+
.. literalinclude:: /includes/fundamentals/code-snippets/crud/geo.rs
127+
:start-after: start-polygon
128+
:end-before: end-polygon
129+
:language: rust
130+
:dedent:
131+
132+
To learn more about the GeoJSON types that you can use in MongoDB, see the
133+
:manual:`GeoJSON </reference/geojson/>` page in the Server manual.
134+
135+
Legacy Coordinate Pairs
136+
~~~~~~~~~~~~~~~~~~~~~~~
137+
138+
Use legacy coordinate pairs to represent geospatial data on a two-dimensional
139+
Euclidean plane.
140+
141+
The following code specifies a legacy coordinate pair that represents the
142+
location of Washington, D.C.:
143+
144+
.. code-block:: rust
145+
:copyable: false
146+
147+
let capital = vec! [-77.0369, 38.9072];
148+
149+
150+
To learn more about legacy coordinate pairs, see
151+
:manual:`Legacy Coordinate Pairs </geospatial-queries/#legacy-coordinate-pairs>`
152+
in the Server manual.
153+
154+
.. _rust-indexes-geo:
155+
156+
Geospatial Indexes
157+
------------------
158+
159+
Before querying geospatial data, you must create an index that corresponds
160+
to the data format. The following index types enable geospatial queries:
161+
162+
- ``2dsphere`` for GeoJSON data
163+
- ``2d`` for legacy coordinate pairs
164+
165+
The following sections on ``2dsphere`` and ``2d`` indexes include code examples
166+
that use the ``theaters`` collection in the ``sample_mflix`` database from the
167+
Atlas sample data.
168+
169+
.. tip::
170+
171+
To learn more about creating an index, see the :ref:`rust-indexes` guide.
172+
173+
For instructions on importing the Atlas sample data, see the :atlas:`Load Sample Data
174+
</sample-data>` page.
175+
176+
2dsphere
177+
~~~~~~~~
178+
179+
To query data stored in the GeoJSON format, add the field containing
180+
both the ``type`` and ``coordinates`` fields to a ``2dsphere`` index. The
181+
following example creates a ``2dsphere`` index on the ``location.geo`` field:
182+
183+
.. io-code-block::
184+
185+
.. input:: /includes/fundamentals/code-snippets/crud/geo.rs
186+
:start-after: start-2dsphere
187+
:end-before: end-2dsphere
188+
:language: rust
189+
:dedent:
190+
191+
.. output::
192+
:language: none
193+
:visible: false
194+
195+
Created index:
196+
location.geo_"2dsphere"
197+
198+
2d
199+
~~
200+
201+
To query data stored as legacy coordinate pairs, add the field containing
202+
legacy coordinate pairs to a ``2d`` index. The following example creates a
203+
``2d`` index on the ``location.geo.coordinates`` field:
204+
205+
.. io-code-block::
206+
207+
.. input:: /includes/fundamentals/code-snippets/crud/geo.rs
208+
:start-after: start-2d
209+
:end-before: end-2d
210+
:language: rust
211+
:dedent:
212+
213+
.. output::
214+
:language: none
215+
:visible: false
216+
217+
Created index:
218+
location.geo.coordinates_"2d"
219+
220+
.. _rust-query-geo:
221+
222+
Geospatial Queries
223+
------------------
224+
225+
After creating a ``2dsphere`` or ``2d`` index on fields containing geospatial data, you
226+
can perform geospatial queries that access those fields.
227+
228+
To query geospatial data, create a query filter with a field name and a geospatial query
229+
operator. You can specify options for certain geospatial query operators to limit
230+
the documents returned.
231+
232+
The following sections on geospatial queries include code examples that use the ``theaters``
233+
collection in the ``sample_mflix`` database from the Atlas sample data. Assume that
234+
the ``theaters`` collection has a ``2dsphere`` index on the ``location.geo`` field.
235+
236+
.. tip::
237+
238+
To learn more about querying data, see the :ref:`rust-query-guide` guide.
239+
240+
For instructions on importing the Atlas sample data, see the :atlas:`Load Sample Data
241+
</sample-data>` page.
242+
243+
Query Operators
244+
~~~~~~~~~~~~~~~
245+
246+
To query your geospatial data, use one of the following query operators:
247+
248+
- ``$near``
249+
- ``$geoWithin``
250+
- ``$nearSphere``
251+
- ``$geoIntersects`` *(requires a 2dsphere index)*
252+
253+
When using the ``$near`` operator, you can specify the following distance operators:
254+
255+
- ``$minDistance``
256+
- ``$maxDistance``
257+
258+
When using the ``$geoWithin`` operator, you can specify the following shape operators:
259+
260+
- ``$box``
261+
- ``$polygon``
262+
- ``$center``
263+
- ``$centerSphere``
264+
265+
.. tip::
266+
267+
To learn more about geospatial query operators, see :manual:`Geospatial Query Operators
268+
</geospatial-queries/#geospatial-query-operators>` in the Server manual.
269+
270+
.. _rust-query-proximity-geo:
271+
272+
Query by Proximity Example
273+
~~~~~~~~~~~~~~~~~~~~~~~~~~
274+
275+
The following example queries for documents in which the ``location.geo`` field
276+
stores a location within 1000 meters of the MongoDB Headquarters in New York City, NY.
277+
The code returns documents in ascending order of their distance from the MongoDB Headquarters.
278+
279+
.. io-code-block::
280+
281+
.. input:: /includes/fundamentals/code-snippets/crud/geo.rs
282+
:start-after: start-proximity
283+
:end-before: end-proximity
284+
:language: rust
285+
:dedent:
286+
287+
.. output::
288+
:language: none
289+
:visible: false
290+
291+
{ "_id":{...},"theaterId":1908,"location":{"address":{...},"geo":{"type":"Point","coordinates":[-73.983487,40.76078] } } }
292+
{ "_id":{...},"theaterId":1448,"location":{"address":{...},"geo":{"type":"Point","coordinates":[-73.982094,40.769882] } } }
293+
294+
.. _rust-query-range-geo:
295+
296+
Query Within a Range Example
297+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
298+
299+
The following example queries for documents in which the ``location.geo`` field
300+
stores a location within the Chicago area. The example creates a vector called ``chicago``
301+
that stores four coordinates representing the bounds of the geographic search area.
302+
303+
.. io-code-block::
304+
305+
.. input:: /includes/fundamentals/code-snippets/crud/geo.rs
306+
:start-after: start-range
307+
:end-before: end-range
308+
:language: rust
309+
:dedent:
310+
311+
.. output::
312+
:language: none
313+
:visible: false
314+
315+
{ "_id":{...},"theaterId":322,"location":{"address":{...},"geo":{ "type":"Point","coordinates":[-87.849403, 41.90707] } } }
316+
{ "_id":{...},"theaterId":2960,"location":{"address":{...},"geo":{ "type":"Point","coordinates":[-87.811262, 41.847938] } } }
317+
{ "_id":{...},"theaterId":323,"location":{"address":{...},"geo":{ "type":"Point","coordinates":[-87.653557, 41.912025] } } }
318+
{ "_id":{...},"theaterId":320,"location":{"address":{...},"geo":{ "type":"Point","coordinates":[-87.805817, 41.847572] } } }
319+
{ "_id":{...},"theaterId":814,"location":{"address":{...},"geo":{ "type":"Point","coordinates":[-87.670631, 41.919514] } } }
320+
321+
.. _rust-addtl-info-geo:
322+
323+
Additional Resources
324+
--------------------
325+
326+
To learn more about find operations, see the :ref:`rust-retrieve-guide` guide.
327+
328+
To learn more about working with geospatial data, see the following Server manual pages:
329+
330+
- :ref:`geo-overview-location-data`
331+
- :manual:`GeoJSON </reference/geojson/>`
332+
333+
API Documentation
334+
~~~~~~~~~~~~~~~~~
335+
336+
To learn more about the methods and types mentioned in this
337+
guide, see the following API documentation:
338+
339+
- `create_index() <{+api+}/struct.Collection.html#method.create_index>`__
340+
- `IndexModel <{+api+}/struct.IndexModel.html>`__
341+
- `find() <{+api+}/struct.Collection.html#method.find>`__

0 commit comments

Comments
 (0)