@@ -25,10 +25,10 @@ raw queries on your MongoDB database. Raw queries allow you to
25
25
query the database by using MongoDB's aggregation pipeline syntax
26
26
rather than Django methods.
27
27
28
- The Django ``QuerySet`` API provides a ``QuerySet. raw()`` method, which allows
28
+ The Django ``QuerySet`` API provides a ``raw()`` method, which allows
29
29
you to perform raw SQL queries on relational databases. However, {+django-odm+}
30
30
does not support the ``raw()`` method. Instead, the ODM provides the
31
- ``QuerySet. raw_aggregate()`` method, which you can use to send instructions
31
+ ``raw_aggregate()`` method, which you can use to send instructions
32
32
to the database in pipeline stages.
33
33
34
34
.. note::
@@ -39,12 +39,22 @@ to the database in pipeline stages.
39
39
the ``aggregate()`` method, see `Aggregation <{+django-docs+}/topics/db/aggregation/>`__
40
40
in the Django documentation.
41
41
42
+ You can run database queries by calling ``QuerySet`` methods on your model's
43
+ ``Manager``. The ``Manager`` class handles database operations and allows you
44
+ to interact with your MongoDB data by referencing Django models. By default,
45
+ Django adds a ``Manager`` named ``objects`` to every model class. This default
46
+ ``Manager`` does not support the ``raw_aggregate()`` method. To use this
47
+ MongoDB-specific method, set your model's ``objects`` field to a custom
48
+ manager called ``MongoManager``.
49
+
42
50
Sample Data
43
51
~~~~~~~~~~~
44
52
45
53
The examples in this guide use the ``Movie`` and ``Theater`` models, which
46
54
represent collections in the ``sample_mflix`` database from the :atlas:`Atlas sample datasets </sample-data>`.
47
- The model classes have the following definitions:
55
+ The ``Movie`` model explicitly sets the ``objects`` field to use a custom ``MongoManager``,
56
+ rather than Django's default ``Manager`` class. The model classes have the following
57
+ definitions:
48
58
49
59
.. literalinclude:: /includes/interact-data/raw-queries.py
50
60
:start-after: start-models
@@ -62,7 +72,7 @@ Run Raw Queries
62
72
---------------
63
73
64
74
To run a raw database query, pass an aggregation pipeline
65
- to the ``QuerySet. raw_aggregate()`` method. Aggregation pipelines
75
+ to the ``raw_aggregate()`` method. Aggregation pipelines
66
76
contain one or more stages that provide instructions on how to
67
77
process documents. After calling the ``raw_aggregate()`` method,
68
78
{+django-odm+} passes your pipeline to the ``pymongo.collection.Collection.aggregate()``
@@ -87,10 +97,9 @@ Filter and Project Document Fields
87
97
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
88
98
89
99
This example runs a raw database query by calling the
90
- ``raw_aggregate()`` method on your ``Movie`` objects,
91
- which represent documents in the ``sample_mflix.movies``
92
- collection. The code passes the following aggregation pipeline stages
93
- to ``raw_aggregate()``:
100
+ ``raw_aggregate()`` method on your ``Movie`` model's ``MongoManager``,
101
+ which queries the ``sample_mflix.movies`` collection. The code
102
+ passes the following aggregation pipeline stages to ``raw_aggregate()``:
94
103
95
104
- ``$match``: Filters for documents that have a ``title``
96
105
field value of ``"The Parent Trap"``
0 commit comments