@@ -14,7 +14,7 @@ Overview
14
14
--------
15
15
16
16
In 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.
18
18
19
19
Geospatial data is data that represents a geographical location on
20
20
the surface of the Earth. Examples of geospatial data include:
@@ -42,8 +42,6 @@ Here is the location of MongoDB headquarters in GeoJSON:
42
42
For definitive information on GeoJSON, see the
43
43
`official IETF specification <https://datatracker.ietf.org/doc/html/rfc7946>`__.
44
44
45
- .. external resource
46
-
47
F438
td>45
GeoJSON Positions
48
46
~~~~~~~~~~~~~~~~~
49
47
@@ -83,25 +81,19 @@ Here are some common GeoJSON types and how you can specify them with positions:
83
81
To learn more about the shapes you can use in MongoDB, see the
84
82
:manual:`GeoJSON manual entry </reference/geojson/>`.
85
83
86
- .. external resource
87
-
88
84
Index
89
85
~~~~~
90
86
91
87
To query data stored in the GeoJSON format, add the field containing
92
88
GeoJSON data to a ``2dsphere`` index. The following snippet creates a
93
89
``2dsphere`` index on the ``location.geo`` field using the ``Indexes`` builder:
94
90
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
99
93
100
94
For more information on the ``Indexes`` builder, see our
101
95
:doc:`guide on the Indexes builder </fundamentals/builders/indexes>`.
102
96
103
- .. guide resource
104
-
105
97
Coordinates on a 2D Plane
106
98
-------------------------
107
99
@@ -125,28 +117,22 @@ To query data stored as legacy coordinate pairs, you must add the field containi
125
117
legacy coordinate pairs to a ``2d`` index. The following snippet creates a
126
118
``2d`` index on the ``coordinates`` field using the ``Indexes`` builder:
127
119
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
132
122
133
123
For more information on the ``Indexes`` builder, see our
134
124
:doc:`guide on the Indexes builder </fundamentals/builders/indexes>`.
135
125
136
126
For more information on legacy coordinate pairs, see the
137
127
:manual:`MongoDB server manual page on legacy coordinate pairs </geospatial-queries/#legacy-coordinate-pairs>`.
138
128
139
- .. external resource
140
-
141
129
.. tip:: Supported Operators
142
130
143
131
Spherical (``2dsphere``) and flat (``2d``) indexes support some, but
144
132
not all, of the same query operators. For a full list of operators
145
133
and their index compatibility, see the
146
134
:manual:`manual entry for geospatial queries </geospatial-queries/#geospatial-query-operators>`.
147
135
148
- .. external resource
149
-
150
136
Geospatial Queries
151
137
------------------
152
138
@@ -163,15 +149,13 @@ To query your geospatial data, use one of the following query operators:
163
149
- ``$nearSphere``
164
150
- ``$geoIntersects`` *requires a 2dsphere index*
165
151
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
167
153
``near()``, ``geoWithin()``, ``nearSphere()``, and ``geoIntersects()`` utility
168
154
methods of the ``Filters`` builder class.
169
155
170
156
For more information on geospatial query operators, see the
171
157
:manual:`manual entry for geospatial queries </geospatial-queries/#geospatial-query-operators>`.
172
158
173
- .. external resource
174
-
175
159
For more information on ``Filters``, see our
176
160
:doc:`guide on the Filters builder </fundamentals/builders/filters>`.
177
161
@@ -180,15 +164,13 @@ Query Parameters
180
164
181
165
To specify a shape to use in a geospatial query, use the
182
166
``Position``, ``Point``, ``LineString``, and ``Polygon`` classes of the MongoDB
183
- Java driver.
167
+ Kotlin driver.
184
168
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
186
170
`GeoJSON package
187
171
<{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/geojson/package-summary.html>`__
188
172
API Documentation.
189
173
190
- .. external resource
191
-
192
174
Examples
193
175
--------
194
176
@@ -197,21 +179,34 @@ to set up your own free-tier Atlas cluster and how to load the sample dataset
197
179
in our :doc:`quick start guide </quick-start>`.
198
180
199
181
The 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.
202
183
203
184
The examples require the following imports:
204
185
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
210
202
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:
213
204
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.
215
210
216
211
Query by Proximity
217
212
~~~~~~~~~~~~~~~~~~
@@ -221,44 +216,40 @@ the ``near()`` static utility method of the ``Filters`` builder class. The
221
216
``near()`` method constructs a query with the ``$near`` query operator.
222
217
223
218
The 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:
226
220
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::
232
222
233
- The output of the code snippet should look something like this:
223
+ .. input:: /examples/generated/GeoTest.snippet.proximity-query.kt
224
+ :language: kotlin
234
225
235
- .. code-block :: json
236
- :copyable: false
226
+ .. output ::
227
+ :language: console
237
228
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)))
250
241
251
242
.. tip:: Fun Fact
252
243
253
244
MongoDB uses the
254
245
:manual:`same reference system </reference/glossary/#std-term-WGS84>`
255
246
as GPS satellites to calculate geometries over the Earth.
256
247
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>`.
259
250
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>`.
262
253
263
254
Query Within a Range
264
255
~~~~~~~~~~~~~~~~~~~~
@@ -271,22 +262,19 @@ The following example searches for movie theaters in a section of Long Island.
271
262
272
263
.. _example_range_query:
273
264
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::
279
266
280
- The output of the code snippet should look something like this:
267
+ .. input:: /examples/generated/GeoTest.snippet.query-range.kt
268
+ :language: kotlin
281
269
282
- .. code-block :: json
283
- :copyable: false
270
+ .. output ::
271
+ :language: console
284
272
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)))
290
278
291
279
The following figure shows the polygon defined by the
292
280
``longIslandTriangle`` variable and dots representing the locations of
@@ -298,9 +286,5 @@ the movie theaters returned by our query.
298
286
For more information on the ``$geoWithin`` operator, see the
299
287
:manual:`reference documentation for $geoWithin </reference/operator/query/geoWithin/>`
300
288
301
- .. external resource
302
-
303
289
For more information on the operators you can use in your query, see the
304
290
:manual:`MongoDB server manual page on geospatial query operators </geospatial-queries/index.html>`
305
-
306
- .. external resource
0 commit comments