@@ -61,7 +61,6 @@ The model classes have the following definitions:
61
61
62
62
class Theater(models.Model):
63
63
theaterId = models.IntegerField(default=0)
64
- location = EmbeddedModelField(Location)
65
64
objects = MongoManager()
66
65
67
66
class Meta:
@@ -70,27 +69,14 @@ The model classes have the following definitions:
70
69
def __str__(self):
71
70
return self.title
72
71
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
-
86
72
To learn how to create a Django application that uses a similar ``Movie``
87
73
model to interact with MongoDB documents, visit the :ref:`django-get-started`
88
74
tutorial.
89
75
90
76
.. _django-raw-queries-run:
91
77
92
- Perform a Raw Query
93
- -------------------
78
+ Run Raw Queries
79
+ ---------------
94
80
95
81
To run a raw database query, pass an aggregation pipeline
96
82
to the ``QuerySet.raw_aggregate()`` method. Aggregation pipelines
@@ -188,9 +174,9 @@ the ``raw_aggregate()`` method.
188
174
You cannot use {+django-odm+} to create Atlas Search indexes.
189
175
190
176
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.
194
180
195
181
For instructions on alternative methods of creating search indexes,
196
182
see :atlas:`Create an Atlas Search Index </atlas-search/tutorial/create-index/>`
@@ -307,55 +293,76 @@ the ``raw_aggregate()`` method:
307
293
You cannot use {+django-odm+} to create ``2d`` or ``2dsphere`` indexes.
308
294
309
295
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.
313
299
314
300
For instructions on using the MongoDB Shell to create geospatial
315
301
indexes, see :manual:`Geospatial Indexes </core/indexes/index-types/index-geospatial/>`
316
302
in the {+mdb-server+} manual.
317
303
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
322
312
323
313
.. io-code-block::
324
314
:copyable: true
325
315
326
316
.. input::
327
317
:language: python
328
318
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
+
329
330
theaters = Theater.objects.raw_aggregate([
330
331
{
331
332
"$match": {
332
333
"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
339
336
}
340
337
}
341
338
}
342
339
},
343
340
{
344
341
"$project": {
345
- "name": 1,
346
- "location": 1
342
+ "theaterId": 1
347
343
}
348
344
}
349
345
])
350
346
351
347
for t in theaters:
352
- print(f"Theater: {t.name}, location : {t.location}\n ")
348
+ print(f"Theater ID : {t.theaterId} ")
353
349
354
350
.. output::
355
351
:language: none
356
352
:visible: false
357
353
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
359
366
360
367
Additional Information
361
368
----------------------
0 commit comments