Skip to content

Commit c550355

Browse files
committed
fix geospatial part
1 parent 52b8ca2 commit c550355

File tree

1 file changed

+43
-36
lines changed

1 file changed

+43
-36
lines changed

source/interact-data/raw-queries.txt

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ The model classes have the following definitions:
6161

6262
class Theater(models.Model):
6363
theaterId = models.IntegerField(default=0)
64-
location = EmbeddedModelField(Location)
6564
objects = MongoManager()
6665

6766
class Meta:
@@ -70,27 +69,14 @@ The model classes have the following definitions:
7069
def __str__(self):
7170
return self.title
7271

73-
class Location(models.Model):
74-
geo = EmbeddedModelField(Geo)
75-
76-
class Meta:
77-
abstract = True
78-
79-
class Geo(models.Model):
80-
type = models.CharField(max_length=100)
81-
coordinates = ArrayField(models.DecimalField(max_digits=9, decimal_places=6), blank=True)
82-
83-
class Meta:
84-
abstract = True
85-
8672
To learn how to create a Django application that uses a similar ``Movie``
8773
model to interact with MongoDB documents, visit the :ref:`django-get-started`
8874
tutorial.
8975

9076
.. _django-raw-queries-run:
9177

92-
Perform a Raw Query
93-
-------------------
78+
Run Raw Queries
79+
---------------
9480

9581
To run a raw database query, pass an aggregation pipeline
9682
to the ``QuerySet.raw_aggregate()`` method. Aggregation pipelines
@@ -188,9 +174,9 @@ the ``raw_aggregate()`` method.
188174
You cannot use {+django-odm+} to create Atlas Search indexes.
189175

190176
For instructions on using the PyMongo driver to create an Atlas
191-
Search index, see :driver:`Atlas Search and Vector Search Indexes
192-
</python/pymongo-driver/current/indexes/atlas-search-index/>` in
193-
the PyMongo documentation.
177+
Search index, see `Atlas Search and Vector Search Indexes
178+
<https://www.mongodb.com/docs/languages/python/pymongo-driver/current/indexes/atlas-search-index/>`__
179+
in the PyMongo documentation.
194180

195181
For instructions on alternative methods of creating search indexes,
196182
see :atlas:`Create an Atlas Search Index </atlas-search/tutorial/create-index/>`
@@ -307,55 +293,76 @@ the ``raw_aggregate()`` method:
307293
You cannot use {+django-odm+} to create ``2d`` or ``2dsphere`` indexes.
308294

309295
For instructions on using the PyMongo driver to create geospatial
310-
indexes, see :driver:`Geospatial Indexes
311-
</python/pymongo-driver/current/indexes/geospatial-index/>` in
312-
the PyMongo documentation.
296+
indexes, see `Geospatial Indexes
297+
<https://www.mongodb.com/docs/languages/python/pymongo-driver/current/indexes/geospatial-index/>`__
298+
in the PyMongo documentation.
313299

314300
For instructions on using the MongoDB Shell to create geospatial
315301
indexes, see :manual:`Geospatial Indexes </core/indexes/index-types/index-geospatial/>`
316302
in the {+mdb-server+} manual.
317303

318-
This example runs a geospatial query by passing the ``$near`` pipeline
319-
stage to the ``raw_aggregate()`` method. The code queries for documents
320-
in which the ``location.geo`` field stores a location within ``1000`` meters
321-
of the MongoDB Headquarters in New York City, NY:
304+
This example runs a geospatial query by passing the ``$match`` and
305+
``$geoWithin`` pipeline stages to the ``raw_aggregate()`` method. The
306+
code performs the following actions:
307+
308+
- Specifies a list of coordinates that represent Chicago's boundaries
309+
- Queries for documents in which the ``location.geo`` field stores a
310+
location within the Chicago area
311+
- Retrieves and prints the ``theaterId`` values of each movie theater in Chicago
322312

323313
.. io-code-block::
324314
:copyable: true
325315

326316
.. input::
327317
:language: python
328318

319+
chicago_bounds = {
320+
"type": "Polygon",
321+
"coordinates": [[
322+
[-87.851, 41.976],
323+
[-87.851, 41.653],
324+
[-87.651, 41.653],
325+
[-87.651, 41.976],
326+
[-87.851, 41.976]
327+
]]
328+
}
329+
329330
theaters = Theater.objects.raw_aggregate([
330331
{
331332
"$match": {
332333
"location.geo": {
333-
"$near": {
334-
"$geometry": {
335-
"type": "Point",
336-
"coordinates": [-73.986805, 40.7620853]
337-
},
338-
"$maxDistance": 1000
334+
"$geoWithin": {
335+
"$geometry": chicago_bounds
339336
}
340337
}
341338
}
342339
},
343340
{
344341
"$project": {
345-
"name": 1,
346-
"location": 1
342+
"theaterId": 1
347343
}
348344
}
349345
])
350346

351347
for t in theaters:
352-
print(f"Theater: {t.name}, location: {t.location}\n")
348+
print(f"Theater ID: {t.theaterId}")
353349

354350
.. output::
355351
:language: none
356352
:visible: false
357353

358-
TODO
354+
Theater ID: 2447
355+
Theater ID: 311
356+
Theater ID: 320
357+
Theater ID: 2960
358+
Theater ID: 2741
359+
Theater ID: 306
360+
Theater ID: 322
361+
Theater ID: 319
362+
Theater ID: 2862
363+
Theater ID: 1777
364+
Theater ID: 814
365+
Theater ID: 323
359366

360367
Additional Information
361368
----------------------

0 commit comments

Comments
 (0)