@@ -14,7 +14,7 @@ Overview
1414--------
1515
1616In this guide, you can learn how to search **geospatial data** with the
17- MongoDB Java Driver, and the different geospatial data formats supported by MongoDB.
17+ MongoDB Kotlin Driver, and the different geospatial data formats supported by MongoDB.
1818
1919Geospatial data is data that represents a geographical location on
2020the surface of the Earth. Examples of geospatial data include:
@@ -42,8 +42,6 @@ Here is the location of MongoDB headquarters in GeoJSON:
4242For definitive information on GeoJSON, see the
4343`official IETF specification <https://datatracker.ietf.org/doc/html/rfc7946>`__.
4444
45- .. external resource
46-
4745GeoJSON Positions
4846~~~~~~~~~~~~~~~~~
4947
@@ -83,25 +81,19 @@ Here are some common GeoJSON types and how you can specify them with positions:
8381To learn more about the shapes you can use in MongoDB, see the
8482:manual:`GeoJSON manual entry </reference/geojson/>`.
8583
86- .. external resource
87-
8884Index
8985~~~~~
9086
9187To query data stored in the GeoJSON format, add the field containing
9288GeoJSON data to a ``2dsphere`` index. The following snippet creates a
9389``2dsphere`` index on the ``location.geo`` field using the ``Indexes`` builder:
9490
95- .. code-block:: java
96-
97- // <MongoCollection setup code here>
98- collection.createIndex(Indexes.geo2dsphere("location.geo"));
91+ .. literalinclude:: /examples/generated/GeoTest.snippet.geo2dsphere-index.kt
92+ :language: kotlin
9993
10094For more information on the ``Indexes`` builder, see our
10195:doc:`guide on the Indexes builder </fundamentals/builders/indexes>`.
10296
103- .. guide resource
104-
10597Coordinates on a 2D Plane
10698-------------------------
10799
@@ -125,28 +117,22 @@ To query data stored as legacy coordinate pairs, you must add the field containi
125117legacy coordinate pairs to a ``2d`` index. The following snippet creates a
126118``2d`` index on the ``coordinates`` field using the ``Indexes`` builder:
127119
128- .. code-block:: java
129-
130- // <MongoCollection setup code here>
131- collection.createIndex(Indexes.geo2d("coordinates"));
120+ .. literalinclude:: /examples/generated/GeoTest.snippet.geo2d-index.kt
121+ :language: kotlin
132122
133123For more information on the ``Indexes`` builder, see our
134124:doc:`guide on the Indexes builder </fundamentals/builders/indexes>`.
135125
136126For more information on legacy coordinate pairs, see the
137127:manual:`MongoDB server manual page on legacy coordinate pairs </geospatial-queries/#legacy-coordinate-pairs>`.
138128
139- .. external resource
140-
141129.. tip:: Supported Operators
142130
143131 Spherical (``2dsphere``) and flat (``2d``) indexes support some, but
144132 not all, of the same query operators. For a full list of operators
145133 and their index compatibility, see the
146134 :manual:`manual entry for geospatial queries </geospatial-queries/#geospatial-query-operators>`.
147135
148- .. external resource
149-
150136Geospatial Queries
151137------------------
152138
@@ -163,15 +149,13 @@ To query your geospatial data, use one of the following query operators:
163149- ``$nearSphere``
164150- ``$geoIntersects`` *requires a 2dsphere index*
165151
166- You can specify these query operators in the MongoDB Java driver with the
152+ You can specify these query operators in the MongoDB Kotlin driver with the
167153``near()``, ``geoWithin()``, ``nearSphere()``, and ``geoIntersects()`` utility
168154methods of the ``Filters`` builder class.
169155
170156For more information on geospatial query operators, see the
171157:manual:`manual entry for geospatial queries </geospatial-queries/#geospatial-query-operators>`.
172158
173- .. external resource
174-
175159For more information on ``Filters``, see our
176160:doc:`guide on the Filters builder </fundamentals/builders/filters>`.
177161
@@ -180,15 +164,13 @@ Query Parameters
180164
181165To specify a shape to use in a geospatial query, use the
182166``Position``, ``Point``, ``LineString``, and ``Polygon`` classes of the MongoDB
183- Java driver.
167+ Kotlin driver.
184168
185- For a full list of the GeoJSON shapes available in the MongoDB Java driver, see the
169+ For a full list of the GeoJSON shapes available in the MongoDB Kotlin driver, see the
186170`GeoJSON package
187171<{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/geojson/package-summary.html>`__
188172API Documentation.
189173
190- .. external resource
191-
192174Examples
193175--------
194176
@@ -197,21 +179,34 @@ to set up your own free-tier Atlas cluster and how to load the sample dataset
197179in our :doc:`quick start guide </quick-start>`.
198180
199181The examples use the ``theaters`` collection in the ``sample_mflix`` database
200- from the sample dataset. The ``theaters`` collection contains a ``2dsphere`` index
201- on the ``location.geo`` field.
182+ from the sample dataset.
202183
203184The examples require the following imports:
204185
205- .. literalinclude:: /includes/fundamentals/code-snippets/Geo.java
206- :language: java
207- :dedent:
208- :start-after: begin exampleImports
209- :end-before: end exampleImports
186+ .. code-block::
187+ :source: kotlin
188+
189+ import com.mongodb.client.model.geojson.Point
190+ import com.mongodb.client.model.geojson.Polygon
191+ import com.mongodb.client.model.geojson.Position
192+ import com.mongodb.client.model.Filters.near
193+ import com.mongodb.client.model.Filters.geoWithin
194+ import com.mongodb.client.model.Projections.fields
195+ import com.mongodb.client.model.Projections.include
196+ import com.mongodb.client.model.Projections.excludeId
197+
198+ The data is modeled using the following Kotlin data class:
199+
200+ .. literalinclude:: /examples/generated/GeoTest.snippet.theater-data-class.kt
201+ :language: kotlin
210202
211- You can find the
212- `source code for the examples on Github here <https://github.com/mongodb/docs-java/blob/master/source/includes/fundamentals/code-snippets/Geo.java>`__.
203+ The results are modeled using the following Kotlin data class:
213204
214- .. external resource
205+ .. literalinclude:: /examples/generated/GeoTest.snippet.results-data-class.kt
206+ :language: kotlin
207+
208+ The ``theaters`` collection already contains a ``2dsphere`` index on the
209+ ``"${Theater::location.name}.${Theater.Location::geo.name}"`` field.
215210
216211Query by Proximity
217212~~~~~~~~~~~~~~~~~~
@@ -221,44 +216,40 @@ the ``near()`` static utility method of the ``Filters`` builder class. The
221216``near()`` method constructs a query with the ``$near`` query operator.
222217
223218The following example queries for theaters between ``10,000`` and ``5,000``
224- meters from the
225- `Great Lawn of Central Park <https://en.wikipedia.org/wiki/Great_Lawn_and_Turtle_Pond>`__.
219+ meters from the Great Lawn of Central Park:
226220
227- .. literalinclude:: /includes/fundamentals/code-snippets/Geo.java
228- :language: java
229- :dedent:
230- :start-after: begin findExample
231- :end-before: end findExample
221+ .. io-code-block::
232222
233- The output of the code snippet should look something like this:
223+ .. input:: /examples/generated/GeoTest.snippet.proximity-query.kt
224+ :language: kotlin
234225
235- .. code-block :: json
236- :copyable: false
226+ .. output ::
227+ :language: console
237228
238- {"location": {"address": {"city": " Bronx"}}}
239- {"location": {"address": {"city": " New York"}}}
240- {"location": {"address": {"city": " New York"}}}
241- {"location": {"address": {"city": " Long Island City"}}}
242- {"location": {"address": {"city": " New York"}}}
243- {"location": {"address": {"city": " Secaucus"}}}
244- {"location": {"address": {"city": " Jersey City"}}}
245- {"location": {"address": {"city": " Elmhurst"}}}
246- {"location": {"address": {"city": " Flushing"}}}
247- {"location": {"address": {"city": " Flushing"}}}
248- {"location": {"address": {"city": " Flushing"}}}
249- {"location": {"address": {"city": " Elmhurst"}}}
229+ TheaterResults(location=Location(address=Address(city= Bronx)))
230+ TheaterResults(location=Location(address=Address(city= New York)))
231+ TheaterResults(location=Location(address=Address(city= New York)))
232+ TheaterResults(location=Location(address=Address(city= Long Island City)))
233+ TheaterResults(location=Location(address=Address(city= New York)))
234+ TheaterResults(location=Location(address=Address(city= Secaucus)))
235+ TheaterResults(location=Location(address=Address(city= Jersey City)))
236+ TheaterResults(location=Location(address=Address(city= Elmhurst)))
237+ TheaterResults(location=Location(address=Address(city= Flushing)))
238+ TheaterResults(location=Location(address=Address(city= Flushing)))
239+ TheaterResults(location=Location(address=Address(city= Flushing)))
240+ TheaterResults(location=Location(address=Address(city= Elmhurst)))
250241
251242.. tip:: Fun Fact
252243
253244 MongoDB uses the
254245 :manual:`same reference system </reference/glossary/#std-term-WGS84>`
255246 as GPS satellites to calculate geometries over the Earth.
256247
257- For more information on the ``$near`` operator, see
258- :manual:`the reference documentation for $near </reference/operator/query/near/#mongodb-query-op.-near>`.
248+ For more information on the ``$near`` operator, see the
249+ :manual:`reference documentation for $near </reference/operator/query/near/#mongodb-query-op.-near>`.
259250
260- For more information on ``Filters``,
261- :doc:`see our guide on the Filters builder </fundamentals/builders/filters>`.
251+ For more information on ``Filters``, see
252+ :doc:`our guide on the Filters builder </fundamentals/builders/filters>`.
262253
263254Query Within a Range
264255~~~~~~~~~~~~~~~~~~~~
@@ -271,22 +262,19 @@ The following example searches for movie theaters in a section of Long Island.
271262
272263.. _example_range_query:
273264
274- .. literalinclude:: /includes/fundamentals/code-snippets/Geo.java
275- :language: java
276- :dedent:
277- :start-after: begin rangeExample
278- :end-before: end rangeExample
265+ .. io-code-block::
279266
280- The output of the code snippet should look something like this:
267+ .. input:: /examples/generated/GeoTest.snippet.query-range.kt
268+ :language: kotlin
281269
282- .. code-block :: json
283- :copyable: false
270+ .. output ::
271+ :language: console
284272
285- {"location": {"address": {"city": " Baldwin"}}}
286- {"location": {"address": {"city": " Levittown"}}}
287- {"location": {"address": {"city": " Westbury"}}}
288- {"location": {"address": {"city": " Mount Vernon"}}}
289- {"location": {"address": {"city": " Massapequa"}}}
273+ TheaterResults(location=Location(address=Address(city= Baldwin))))
274+ TheaterResults(location=Location(address=Address(city= Levittown)))
275+ TheaterResults(location=Location(address=Address(city= Westbury)))
276+ TheaterResults(location=Location(address=Address(city= Mount Vernon)))
277+ TheaterResults(location=Location(address=Address(city= Massapequa)))
290278
291279The following figure shows the polygon defined by the
292280``longIslandTriangle`` variable and dots representing the locations of
@@ -298,9 +286,5 @@ the movie theaters returned by our query.
298286For more information on the ``$geoWithin`` operator, see the
299287:manual:`reference documentation for $geoWithin </reference/operator/query/geoWithin/>`
300288
301- .. external resource
302-
303289For more information on the operators you can use in your query, see the
304290:manual:`MongoDB server manual page on geospatial query operators </geospatial-queries/index.html>`
305-
306- .. external resource
0 commit comments