@@ -18,34 +18,34 @@ queries that span multiple collections.
18
18
``QuerySet.explain() ``
19
19
======================
20
20
21
- - :meth: `QuerySet.explain() <django.db.models.query.QuerySet.explain> ` supports
22
- the `comment and verbosity options
23
- <https://www.mongodb.com/docs/manual/reference/command/explain/#command-fields> `_.
21
+ :meth: `QuerySet.explain() <django.db.models.query.QuerySet.explain> ` supports
22
+ the `comment and verbosity options
23
+ <https://www.mongodb.com/docs/manual/reference/command/explain/#command-fields> `_.
24
24
25
- Example::
25
+ Example::
26
26
27
- Model.objects.explain(comment="...", verbosity="...")
27
+ Model.objects.explain(comment="...", verbosity="...")
28
28
29
- Valid values for ``verbosity `` are ``"queryPlanner" `` (default),
30
- ``"executionStats" ``, and ``"allPlansExecution" ``.
29
+ Valid values for ``verbosity `` are ``"queryPlanner" `` (default),
30
+ ``"executionStats" ``, and ``"allPlansExecution" ``.
31
31
32
32
MongoDB-specific ``QuerySet `` methods
33
33
=====================================
34
34
35
35
.. class :: django_mongodb_backend.managers.MongoManager
36
36
37
- Some MongoDB-specific ``QuerySet `` methods are available by adding a custom
38
- :class: `~django.db.models.Manager `, ``MongoManager ``, to your model::
37
+ Some MongoDB-specific ``QuerySet `` methods are available by adding a custom
38
+ :class: `~django.db.models.Manager `, ``MongoManager ``, to your model::
39
39
40
- from django.db import models
40
+ from django.db import models
41
41
42
- from django_mongodb_backend.managers import MongoManager
42
+ from django_mongodb_backend.managers import MongoManager
43
43
44
44
45
- class MyModel(models.Model):
46
- ...
45
+ class MyModel(models.Model):
46
+ ...
47
47
48
- objects = MongoManager()
48
+ objects = MongoManager()
49
49
50
50
51
51
.. currentmodule :: django_mongodb_backend.queryset.MongoQuerySet
@@ -55,48 +55,48 @@ Some MongoDB-specific ``QuerySet`` methods are available by adding a custom
55
55
56
56
.. method :: raw_aggregate(pipeline, using=None)
57
57
58
- Similar to :meth: `QuerySet.raw()<django.db.models.query.QuerySet.raw> `, but
59
- instead of a raw SQL query, this method accepts a pipeline that will be passed
60
- to :meth: `pymongo.collection.Collection.aggregate `.
61
-
62
- For example, you could write a custom match criteria::
63
-
64
- Question.objects.raw_aggregate([{"$match": {"question_text": "What's up"}}])
65
-
66
- The pipeline may also return additional fields that will be added as
67
- annotations on the models::
68
-
69
- >>> questions = Question.objects.raw_aggregate([{
70
- ... "$project": {
71
- ... "question_text": 1,
72
- ... "pub_date": 1,
73
- ... "year_published": {"$year": "$pub_date"}
74
- ... }
75
- ... }])
76
- >>> for q in questions:
77
- ... print(f"{q.question_text} was published in {q.year_published}.")
78
- ...
79
- What's up? was published in 2024.
80
-
81
- Fields may also be left out:
82
-
83
- >>> Question.objects.raw_aggregate([{" $project" : {" question_text" : 1 }}])
84
-
85
- The ``Question `` objects returned by this query will be deferred model instances
86
- (see :meth: `~django.db.models.query.QuerySet.defer() `). This means that the
87
- fields that are omitted from the query will be loaded on demand. For example::
88
-
89
- >>> for q in Question.objects.raw_aggregate([{"$project": {"question_text": 1}}]):
90
- >>> print(
91
- ... q.question_text, # This will be retrieved by the original query.
92
- ... q.pub_date, # This will be retrieved on demand.
93
- ... )
94
- ...
95
- What's new 2023-09-03 12:00:00+00:00
96
- What's up 2024-08-23 20:57:30+00:00
97
-
98
- From outward appearances, this looks like the query has retrieved both the
99
- question text and published date. However, this example actually issued three
100
- queries. Only the question texts were retrieved by the ``raw_aggregate() ``
101
- query -- the published dates were both retrieved on demand when they were
102
- printed.
58
+ Similar to :meth: `QuerySet.raw()<django.db.models.query.QuerySet.raw> `, but
59
+ instead of a raw SQL query, this method accepts a pipeline that will be passed
60
+ to :meth: `pymongo.collection.Collection.aggregate `.
61
+
62
+ For example, you could write a custom match criteria::
63
+
64
+ Question.objects.raw_aggregate([{"$match": {"question_text": "What's up"}}])
65
+
66
+ The pipeline may also return additional fields that will be added as
67
+ annotations on the models::
68
+
69
+ >>> questions = Question.objects.raw_aggregate([{
70
+ ... "$project": {
71
+ ... "question_text": 1,
72
+ ... "pub_date": 1,
73
+ ... "year_published": {"$year": "$pub_date"}
74
+ ... }
75
+ ... }])
76
+ >>> for q in questions:
77
+ ... print(f"{q.question_text} was published in {q.year_published}.")
78
+ ...
79
+ What's up? was published in 2024.
80
+
81
+ Fields may also be left out:
82
+
83
+ >>> Question.objects.raw_aggregate([{" $project" : {" question_text" : 1 }}])
84
+
85
+ The ``Question `` objects returned by this query will be deferred model instances
86
+ (see :meth: `~django.db.models.query.QuerySet.defer() `). This means that the
87
+ fields that are omitted from the query will be loaded on demand. For example::
88
+
89
+ >>> for q in Question.objects.raw_aggregate([{"$project": {"question_text": 1}}]):
90
+ >>> print(
91
+ ... q.question_text, # This will be retrieved by the original query.
92
+ ... q.pub_date, # This will be retrieved on demand.
93
+ ... )
94
+ ...
95
+ What's new 2023-09-03 12:00:00+00:00
96
+ What's up 2024-08-23 20:57:30+00:00
97
+
98
+ From outward appearances, this looks like the query has retrieved both the
99
+ question text and published date. However, this example actually issued three
100
+ queries. Only the question texts were retrieved by the ``raw_aggregate() ``
101
+ query -- the published dates were both retrieved on demand when they were
102
+ printed.
0 commit comments