Skip to content

Commit f69bda4

Browse files
authored
DOCSP-38210: geospatial (#19)
1 parent 8bfcf13 commit f69bda4

File tree

2 files changed

+82
-8
lines changed

2 files changed

+82
-8
lines changed

source/tutorials.txt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ Tutorials
1515
/tutorials/aggregation/
1616
/tutorials/change-stream/
1717
/tutorials/text-search/
18+
/tutorials/geospatial/
1819

19-
..
20-
/tutorials/geo/
21-
/tutorials/gridfs/
22-
/tutorials/command/
20+
.. /tutorials/gridfs/
21+
.. /tutorials/command/
2322

2423
- :ref:`scala-connect`
2524
- :ref:`scala-db-coll`
@@ -30,8 +29,7 @@ Tutorials
3029
- :ref:`scala-aggregation`
3130
- :ref:`scala-changestream`
3231
- :ref:`scala-text-search`
32+
- :ref:`scala-geospatial`
3333

34-
..
35-
- :ref:`scala-geo`
36-
- :ref:`scala-gridfs`
37-
- :ref:`scala-run-command`
34+
.. - :ref:`scala-gridfs`
35+
.. - :ref:`scala-run-command`

source/tutorials/geospatial.txt

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
.. _scala-geospatial:
2+
3+
=================
4+
Geospatial Search
5+
=================
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: code example, search coordinates, location
13+
14+
.. contents:: On this page
15+
:local:
16+
:backlinks: none
17+
:depth: 2
18+
:class: singlecol
19+
20+
To support geospatial queries, MongoDB provides geospatial
21+
indexes and geospatial query operators.
22+
23+
To learn more about performing geospatial queries, see
24+
:manual:`Geospatial Queries </geospatial-queries/>` in the
25+
Server manual.
26+
27+
Prerequisites
28+
-------------
29+
30+
.. include:: /includes/prereq-restaurants.rst
31+
32+
.. code-block:: scala
33+
34+
import org.mongodb.scala._
35+
import org.mongodb.scala.model.geojson._
36+
import org.mongodb.scala.model.Indexes
37+
import org.mongodb.scala.model.Filters
38+
39+
.. include:: /includes/obs-note.rst
40+
41+
Connect to a MongoDB Deployment
42+
-------------------------------
43+
44+
.. include:: /includes/connect-section.rst
45+
46+
Create a 2dsphere Index
47+
-----------------------
48+
49+
To create a ``2dsphere`` index, use the ``Indexes.geo2dsphere()``
50+
helper to create a specification for the ``2dsphere`` index. Pass the
51+
specification to the ``MongoCollection.createIndex()`` method to create
52+
the index.
53+
54+
The following example creates a ``2dsphere`` index on the
55+
``"contact.location"`` field in the collection:
56+
57+
.. code-block:: scala
58+
59+
collection.createIndex(Indexes.geo2dsphere("contact.location")).printResults()
60+
61+
Query for Locations Near a GeoJSON Point
62+
----------------------------------------
63+
64+
MongoDB provides various geospatial query operators. To facilitate
65+
the creation of geospatial query filters, the driver provides
66+
the ``Filters`` class and the ``com.mongodb.client.model.geojson``
67+
package.
68+
69+
The following example returns documents that are at least ``1000.0`` meters
70+
and at most ``5000.0`` meters from the specified GeoJSON ``Point`` instance,
71+
automatically sorted from nearest to farthest:
72+
73+
.. code-block:: scala
74+
75+
val refPoint = Point(Position(-73.9667, 40.78))
76+
collection.find(Filters.near("contact.location", refPoint, 5000.0, 1000.0)).printResults()

0 commit comments

Comments
 (0)