@@ -39,10 +39,10 @@ Geospatial Data Formats
39
39
All geospatial data in MongoDB is stored in one of the following formats:
40
40
41
41
- GeoJSON, a format that represents geospatial data on an earth-like
42
- sphere.
42
+ sphere
43
43
44
44
- Legacy coordinate pairs, a format that represents geospatial data
45
- on a Euclidean plane.
45
+ on a Euclidean plane
46
46
47
47
GeoJSON
48
48
~~~~~~~
@@ -88,8 +88,8 @@ Here are some common GeoJSON types and how you can specify them with positions:
88
88
.. code-block:: python
89
89
90
90
{
91
- "type": "Point",
92
- "coordinates": [-73.856077, 40.848447]
91
+ "type": "Point",
92
+ "coordinates": [-73.856077, 40.848447]
93
93
}
94
94
95
95
- ``LineString``: an array of two or more positions that forms a series of line
@@ -100,12 +100,12 @@ Here are some common GeoJSON types and how you can specify them with positions:
100
100
.. code-block:: python
101
101
102
102
{
103
- "type": "LineString",
104
- "coordinates":
105
- [[116.572, 40.430],
106
- [116.570, 40.434],
107
- [116.567, 40.436],
108
- [116.566, 40.441]]
103
+ "type": "LineString",
104
+ "coordinates":
105
+ [[116.572, 40.430],
106
+ [116.570, 40.434],
107
+ [116.567, 40.436],
108
+ [116.566, 40.441]]
109
109
}
110
110
111
111
- ``Polygon``: an array of positions in which the first and last
@@ -115,13 +115,13 @@ Here are some common GeoJSON types and how you can specify them with positions:
115
115
.. code-block:: python
116
116
117
117
{
118
- "type": "Polygon",
119
- "coordinates":
120
- [[[12.446086, 41.901977],
121
- [12.457952, 41.901559],
122
- [12.455375, 41.907351],
123
- [12.449863, 41.905186],
124
- [12.446086, 41.901977]]]
118
+ "type": "Polygon",
119
+ "coordinates":
120
+ [[[12.446086, 41.901977],
121
+ [12.457952, 41.901559],
122
+ [12.455375, 41.907351],
123
+ [12.449863, 41.905186],
124
+ [12.446086, 41.901977]]]
125
125
}
126
126
127
127
To learn more about the GeoJSON types you can use in MongoDB, see the
@@ -136,7 +136,7 @@ Legacy Coordinate Pairs
136
136
Use legacy coordinate pairs to store data that represents geospatial information
137
137
on a two-dimensional plane.
138
138
139
- Legacy coordinate pairs are represented by an array of two values, in which the first
139
+ Legacy coordinate pairs are represented by an array of two values, in which the first value
140
140
represents the ``x`` axis value and the second represents the ``y`` axis value.
141
141
142
142
For more information on legacy coordinate pairs, see the
@@ -200,88 +200,94 @@ The following example queries for documents with a ``location.geo`` field value
200
200
within 1000 meters of the MongoDB Headquarters in New York City, NY. It returns documents
201
201
from nearest to farthest.
202
202
203
- .. code-block:: python
204
-
205
- # set query with point at MongoDB headquarters and a maxDistance of 1000 meters
206
- query = {
207
- "location.geo": {
208
- "$near": {
209
- "$geometry": {
210
- "type": "Point",
211
- "coordinates": [-73.986805, 40.7620853] # Search around this location
212
- },
213
- "$maxDistance": 1000 # Distance in meters (1 km)
214
- }
215
- }
216
- }
217
-
218
- # fetches the _id and theaterId fields
219
- projection = { "theaterId": 1 }
203
+ .. io-code-block::
204
+ :copyable: true
205
+
206
+ .. input::
207
+ :language: python
208
+
209
+ # set query with point at MongoDB headquarters and a maxDistance of 1000 meters
210
+ query = {
211
+ "location.geo": {
212
+ "$near": {
213
+ "$geometry": {
214
+ "type": "Point",
215
+ "coordinates": [-73.986805, 40.7620853] # Search around this location
216
+ },
217
+ "$maxDistance": 1000 # Distance in meters (1 km)
218
+ }
219
+ }
220
+ }
220
221
221
- nearby_places = location.find(query, projection)
222
+ # fetches the _id and theaterId fields
223
+ projection = { "theaterId": 1 }
222
224
223
- for i in nearby_places:
224
- print(i)
225
+ nearby_places = location.find(query, projection)
225
226
226
- The results of the preceding example contain the following documents:
227
+ for i in nearby_places:
228
+ print(i)
227
229
228
- .. code-block:: json
229
- :copyable: False
230
+ .. output::
231
+ :language: json
232
+ :visible: false
230
233
231
- { "_id" : ObjectId("59a47287cfa9a3a73e51e8e2"), "theaterId" : 1908 }
232
- { "_id" : ObjectId("59a47286cfa9a3a73e51e838"), "theaterId" : 1448 }
234
+ { "_id" : ObjectId("59a47287cfa9a3a73e51e8e2"), "theaterId" : 1908 }
235
+ { "_id" : ObjectId("59a47286cfa9a3a73e51e838"), "theaterId" : 1448 }
233
236
234
237
Query by Polygon
235
238
~~~~~~~~~~~~~~~~
236
239
237
240
The following example queries for documents with a ``location.geo`` field value that exists
238
241
within the boundaries of Manhattan.
239
242
240
- .. code-block:: python
241
-
242
- # Polygon representation of Manhattan
243
- query = {
244
- "location.geo": {
245
- "$geoWithin": {
246
- "$geometry": {
247
- # Search around this location
248
- "type": "Polygon",
249
- "coordinates":
250
- [[[-73.925492, 40.877410],
251
- [-73.910372, 40.872366],
252
- [-73.935127, 40.834020],
253
- [-73.929049, 40.798569],
254
- [-73.976485, 40.711432],
255
- [-74.015747, 40.701229],
256
- [-74.018859, 40.708367],
257
- [-74.008007, 40.754307],
258
- [-73.925492, 40.877410]]]
259
- }
260
- }
261
- }
262
- }
263
-
264
- # fetches the _id and theaterId fields
265
- projection = { "theaterId": 1 }
266
-
267
- nearby_places = location.find(query, projection)
268
-
269
- for i in nearby_places:
270
- print(i)
271
-
272
- The results of the preceding example contain the following documents:
273
-
274
- .. code-block:: json
275
- :copyable: False
276
-
277
- { "_id" : ObjectId("59a47287cfa9a3a73e51e8e2"), "theaterId" : 1908 }
278
- { "_id" : ObjectId("59a47287cfa9a3a73e51eccb"), "theaterId" : 835 }
279
- { "_id" : ObjectId("59a47286cfa9a3a73e51e838"), "theaterId" : 1448 }
280
- { "_id" : ObjectId("59a47286cfa9a3a73e51e744"), "theaterId" : 1028 }
281
- { "_id" : ObjectId("59a47287cfa9a3a73e51ebe1"), "theaterId" : 609 }
282
- { "_id" : ObjectId("59a47287cfa9a3a73e51e8ed"), "theaterId" : 1906 }
283
- { "_id" : ObjectId("59a47287cfa9a3a73e51e87d"), "theaterId" : 1531 }
284
- { "_id" : ObjectId("59a47287cfa9a3a73e51eb63"), "theaterId" : 482 }
243
+ .. io-code-block::
244
+ :copyable: true
245
+
246
+ .. input::
247
+ :language: python
248
+
249
+ # Polygon representation of Manhattan
250
+ query = {
251
+ "location.geo": {
252
+ "$geoWithin": {
253
+ "$geometry": {
254
+ # Search around this location
255
+ "type": "Polygon",
256
+ "coordinates":
257
+ [[[-73.925492, 40.877410],
258
+ [-73.910372, 40.872366],
259
+ [-73.935127, 40.834020],
260
+ [-73.929049, 40.798569],
261
+ [-73.976485, 40.711432],
262
+ [-74.015747, 40.701229],
263
+ [-74.018859, 40.708367],
264
+ [-74.008007, 40.754307],
265
+ [-73.925492, 40.877410]]]
266
+ }
267
+ }
268
+ }
269
+ }
270
+
271
+ # fetches the _id and theaterId fields
272
+ projection = { "theaterId": 1 }
273
+
274
+ nearby_places = location.find(query, projection)
275
+
276
+ for i in nearby_places:
277
+ print(i)
278
+
279
+ .. output::
280
+ :language: json
281
+ :visible: false
282
+
283
+ { "_id" : ObjectId("59a47287cfa9a3a73e51e8e2"), "theaterId" : 1908 }
284
+ { "_id" : ObjectId("59a47287cfa9a3a73e51eccb"), "theaterId" : 835 }
285
+ { "_id" : ObjectId("59a47286cfa9a3a73e51e838"), "theaterId" : 1448 }
286
+ { "_id" : ObjectId("59a47286cfa9a3a73e51e744"), "theaterId" : 1028 }
287
+ { "_id" : ObjectId("59a47287cfa9a3a73e51ebe1"), "theaterId" : 609 }
288
+ { "_id" : ObjectId("59a47287cfa9a3a73e51e8ed"), "theaterId" : 1906 }
289
+ { "_id" : ObjectId("59a47287cfa9a3a73e51e87d"), "theaterId" : 1531 }
290
+ { "_id" : ObjectId("59a47287cfa9a3a73e51eb63"), "theaterId" : 482 }
285
291
286
292
Additional Resources
287
293
--------------------
0 commit comments