@@ -23,29 +23,32 @@ Overview
23
23
In this guide, you can learn how to use {+django-odm+} to specify
24
24
a database query.
25
25
26
+ You can refine the set of documents that a query returns by creating a
27
+ **query filter**. A query filter is an expression that specifies the search
28
+ criteria MongoDB uses to match documents in a read or write operation.
29
+ In a query filter, you can prompt {+django-odm+} to search for documents with an
30
+ exact match to your query, or you can compose query filters to express more
31
+ complex matching criteria.
32
+
33
+ Query API
34
+ ~~~~~~~~~
35
+
26
36
The Django ``QuerySet`` API provides methods that allow you to retrieve
27
37
model objects. To run a MongoDB database query, call ``QuerySet`` methods
28
- on your model's ``Manager`` . The ``Manager`` class handles database
38
+ on your model's manager . The ``Manager`` class handles database
29
39
operations and allows you to interact with your MongoDB data by referencing
30
40
Django models. By default, Django adds a ``Manager`` named ``objects``
31
41
to every model class.
32
42
33
43
When you assign a ``QuerySet`` to a variable, {+django-odm+} does not
34
44
perform the operation until you evaluate the variable. After
35
45
evaluating the ``QuerySet``, Django saves the query results in the ``QuerySet``
36
- cache. Future evaluations of the ``QuerySet`` reuse the cached results.
46
+ cache. Future evaluations of the ``QuerySet`` use the cached results.
37
47
38
48
.. tip::
39
49
40
- To learn more about the Django ``QuerySet`` API, see `QuerySet API reference
41
- <{+django-docs+}/ref/models/querysets/>`__ in the Django documentation.
42
-
43
- You can refine the set of documents that a query returns by creating a
44
- **query filter**. A query filter is an expression that specifies the search
45
- criteria MongoDB uses to match documents in a read or write operation.
46
- In a query filter, you can prompt {+django-odm+} to search for documents with an
47
- exact match to your query, or you can compose query filters to express more
48
- complex matching criteria.
50
+ To learn more about the Django ``QuerySet`` API, see `QuerySet API reference
51
+ <{+django-docs+}/ref/models/querysets/>`__ in the Django documentation.
49
52
50
53
Sample Data
51
54
~~~~~~~~~~~
@@ -86,7 +89,7 @@ Run a Query
86
89
-----------
87
90
88
91
To query your MongoDB data, call a ``QuerySet`` method on your
89
- model's ``Manager`` class and specify your matching criteria in a
92
+ model's manager and specify your matching criteria in a
90
93
query filter.
91
94
92
95
This section describes how to use the following methods from
@@ -104,7 +107,7 @@ Retrieve All Documents
104
107
~~~~~~~~~~~~~~~~~~~~~~
105
108
106
109
To retrieve all documents from a collection, call the ``all()``
107
- method on your model's ``Manager`` class .
110
+ method on your model's manager .
108
111
109
112
The following example calls the ``all()`` method to retrieve
110
113
all documents in the ``sample_mflix.movies`` collection:
@@ -137,7 +140,7 @@ Retrieve Matching Documents
137
140
~~~~~~~~~~~~~~~~~~~~~~~~~~~
138
141
139
142
To query a collection for documents that match a set of criteria,
140
- call the ``filter()`` method on your model's ``Manager`` class .
143
+ call the ``filter()`` method on your model's manager .
141
144
Pass a query filter to the ``filter()`` method that specifies your
142
145
query criteria.
143
146
@@ -165,9 +168,8 @@ Retrieve One Document
165
168
~~~~~~~~~~~~~~~~~~~~~
166
169
167
170
To retrieve one document from a collection, call the ``get()``
168
- method on your model's ``Manager`` class. Pass
169
- a query filter to the ``get()`` method that specifies your
170
- query criteria.
171
+ method on your model's manager. Pass a query filter to
172
+ the ``get()`` method that specifies your query criteria.
171
173
172
174
.. important::
173
175
@@ -176,7 +178,6 @@ query criteria.
176
178
that might match multiple, use the :ref:`first() <django-query-first>`
177
179
method.
178
180
179
-
180
181
The following example calls the ``get()`` method
181
182
to retrieve one document that has a ``title``
182
183
value of ``"Finding Nemo"``:
@@ -242,13 +243,15 @@ Run Raw Database Queries
242
243
~~~~~~~~~~~~~~~~~~~~~~~~
243
244
244
245
If you want to run complex queries that Django's query API
245
- cannot express , you can use the ``raw_aggregate()`` method. This
246
+ does not support , you can use the ``raw_aggregate()`` method. This
246
247
method allows you to specify your query criteria in a MongoDB
247
248
aggregation pipeline, which you pass as an argument to
248
249
``raw_aggregate()``.
249
250
250
- To learn how to run raw database queries, see the :ref:`django-raw-queries`
251
- guide.
251
+ .. TODO: To learn how to run raw database queries, see the :ref:`django-raw-queries`
252
+ guide.
253
+
254
+ .. _django-query-filter:
252
255
253
256
Customize Your Query Filter
254
257
---------------------------
@@ -271,7 +274,7 @@ expression matching, and year value matching for datetime fields.
271
274
272
275
To view a full list of lookup types, see `Field lookups
273
276
<{+django-docs+}/ref/models/querysets/#field-lookups>`__ in the
274
- ``QuerySet`` Django documentation .
277
+ ``QuerySet`` Django API reference .
275
278
276
279
This section describes how to refine your query filters
277
280
in the following ways:
@@ -296,33 +299,33 @@ matching criteria for text values:
296
299
* - Lookup Type
297
300
- Description
298
301
299
- * - ``__exact ``
302
+ * - ``exact ``
300
303
- | Specifies an exact text match. If you do not provide a
301
304
lookup type in your query filter, {+django-odm+} uses this
302
305
type by default.
303
306
304
307
| You can specify a case-insensitive exact text match
305
- by using ``__iexact ``.
306
- * - ``__contains ``
308
+ by using ``iexact ``.
309
+ * - ``contains ``
307
310
- | Specifies a partial text match.
308
311
309
312
| You can specify a case-insensitive partial text match
310
- by using ``__icontains ``.
311
- * - ``__startswith ``
313
+ by using ``icontains ``.
314
+ * - ``startswith ``
312
315
- | Matches field values that begin with the specified text.
313
316
314
317
| You can specify a case-insensitive partial text match
315
- by using ``__istartswith ``.
316
- * - ``__endswith ``
318
+ by using ``istartswith ``.
319
+ * - ``endswith ``
317
320
- | Matches field values that end with the specified text.
318
321
319
322
| You can specify a case-insensitive partial text match
320
- by using ``__iendswith ``.
323
+ by using ``iendswith ``.
321
324
322
325
Example
323
326
```````
324
327
325
- The following example uses the ``__contains `` lookup to
328
+ The following example uses the ``contains `` lookup to
326
329
query for documents in which the ``plot`` value
327
330
includes the text ``"coming-of-age"``:
328
331
@@ -363,27 +366,27 @@ against a specified value:
363
366
* - Lookup Type
364
367
- Description
365
368
366
- * - ``__gt ``
369
+ * - ``gt ``
367
370
- | Matches field values that are greater than the
368
371
specified value.
369
372
370
- * - ``__gte ``
373
+ * - ``gte ``
371
374
- | Matches field values that are greater than or equal
372
375
to the specified value.
373
376
374
- * - ``__lt ``
377
+ * - ``lt ``
375
378
- | Matches field values that are less than the specified
376
379
value.
377
380
378
- * - ``__lte ``
381
+ * - ``lte ``
379
382
- | Matches field values that are less or equal to than the specified
380
383
value.
381
384
382
385
383
386
Example
384
387
```````
385
388
386
- The following example uses the ``__lte `` lookup to
389
+ The following example uses the ``lte `` lookup to
387
390
query for documents in which the ``runtime`` value
388
391
is less than or equal to ``50``:
389
392
@@ -488,12 +491,15 @@ You can provide a sort order for your query results by using the
488
491
of a given field, pass the field name as an argument. To specify a descending
489
492
sort, pass the field name prefixed with a minus symbol (``-``) as an argument.
490
493
494
+ You can pass multiple field names, separated by commas, to the ``order_by()`` method.
495
+ {+django-odm+} sorts results by each field in the order provided.
496
+
491
497
Example
492
498
```````
493
499
494
500
This example performs the following actions:
495
501
496
- - Calls the ``filter()`` method on the ``Movie`` model's ``Manager`` to query
502
+ - Calls the ``filter()`` method on the ``Movie`` model's manager to query
497
503
the ``sample_mflix.movies`` collection
498
504
- Queries documents that have a ``title`` value starting
499
505
with the text ``"Rocky"``
@@ -517,8 +523,8 @@ This example performs the following actions:
517
523
518
524
.. _django-query-limit:
519
525
520
- Limit Results
521
- ~~~~~~~~~~~~~
526
+ Skip and Limit Results
527
+ ~~~~~~~~~~~~~~~~~~~~~~
522
528
523
529
You can specify the number of results that a query returns
524
530
by using Python's array-slicing syntax, as shown
@@ -531,7 +537,7 @@ in the following code:
531
537
532
538
Replace the ``<start>`` and ``<end>`` placeholders with integer
533
539
values representing the subset of results you want to return.
534
- The start value is non-inclusive and the end value is inclusive.
540
+ The start value is exclusive and the end value is inclusive.
535
541
536
542
If you omit the ``<start>`` value, the query returns each result,
537
543
beginning with the first match, until it returns the number specified
@@ -618,16 +624,19 @@ field you want to query, as shown in the following code:
618
624
619
625
Model.objects.filter(<model field>__<embedded model field>=<value>)
620
626
627
+ You can also use :ref:`field lookups <django-query-filter>` to refine
628
+ your embedded model query. Add your lookup type after the embedded
629
+ model field you're querying, prefixed by double underscores.
630
+
621
631
Example
622
632
```````
623
633
624
- This example uses a lookup that spans the ``Movie`` and ``Award``
625
- relationship. The ``Movie`` model's ``awards`` field has an
626
- embedded ``Award`` model value. This embedded model represents
627
- the ``awards`` object field in the ``sample_mflix.movies`` collection
634
+ This example uses a lookup to query the ``Award`` model, which is embedded
635
+ in the ``Movie`` model. This embedded model represents the
636
+ ``awards`` field in the ``sample_mflix.movies`` collection
628
637
and its nested ``wins``, ``nominations``, and ``text`` fields. The
629
- following code uses a lookup to query for documents in which the ``awards.wins``
630
- nested field value is ``93 ``:
638
+ following code queries for documents in which the ``awards.wins``
639
+ nested field value is greater than ``150 ``:
631
640
632
641
.. io-code-block::
633
642
:copyable:
@@ -640,17 +649,21 @@ nested field value is ``93``:
640
649
.. output::
641
650
:visible: false
642
651
643
- <QuerySet [<Movie: The Queen>, <Movie: Dallas Buyers Club>]>
652
+ <QuerySet [<Movie: The Lord of the Rings: The Return of the King>,
653
+ <Movie: No Country for Old Men>, <Movie: Slumdog Millionaire>,
654
+ <Movie: Boyhood>, <Movie: The Social Network>, <Movie: Inception>,
655
+ <Movie: Gravity>, <Movie: Gravity>, <Movie: The Artist>,
656
+ <Movie: 12 Years a Slave>, <Movie: Birdman: Or (The Unexpected Virtue of Ignorance)>]>
644
657
645
658
.. _django-query-arrayfield:
646
659
647
660
Query an ArrayField
648
- ~~~~~~~~~~~~~~~~~
661
+ ~~~~~~~~~~~~~~~~~~~
649
662
650
663
You can query data stored in an ``ArrayField`` value
651
664
by using the standard query syntax. {+django-odm+} provides
652
665
additional field lookup types for querying ``ArrayField`` values,
653
- as described in the following table:
666
+ which are described in the following table:
654
667
655
668
.. list-table::
656
669
:header-rows: 1
@@ -659,27 +672,27 @@ as described in the following table:
659
672
* - Lookup Type
660
673
- Description
661
674
662
- * - ``__contains ``
675
+ * - ``contains ``
663
676
- | Matches fields that store the provided value as
664
677
a subset of their data. This ``ArrayField`` lookup overrides the
665
- ``__contains `` lookup described in the :ref:`django-query-text-match`
678
+ ``contains `` lookup described in the :ref:`django-query-text-match`
666
679
section of this guide.
667
680
668
- * - ``__contained_by ``
681
+ * - ``contained_by ``
669
682
- | Matches fields that store a subset of the provided values.
670
- This lookup type is the inverse of ``__contains ``.
683
+ This lookup type is the inverse of ``contains ``.
671
684
672
- * - ``__overlap ``
685
+ * - ``overlap ``
673
686
- | Matches field that store any of the provided values.
674
687
675
- * - ``__len ``
688
+ * - ``len ``
676
689
- | Matches fields that store the number of values provided.
677
690
678
691
Example
679
692
```````
680
693
681
- The following example uses the ``__overlap `` lookup to query
682
- for documents whose ``genres`` field stores any values in the
694
+ The following example uses the ``overlap `` lookup to query
695
+ for documents whose ``genres`` field contains any values in the
683
696
``["Adventure", "Family"]`` array:
684
697
685
698
.. io-code-block::
@@ -713,7 +726,7 @@ section. Chain each field and nested field name together, separated
713
726
by double underscores (``__``), until you reach the field you want
714
727
to query.
715
728
716
- The following example queries the ``imdb`` field, a ``JSONField``,
729
+ The following example queries the ``JSONField``-valued ``imdb`` field
717
730
for values of its nested ``votes`` field that exceed ``900000``:
718
731
719
732
.. io-code-block::
@@ -811,7 +824,7 @@ for a document whose primary key is ``ObjectId("573a1394f29313caabce0d37")``:
811
824
812
825
The ``Movie`` model, which represents the ``sample_mflix.movies`` collection,
813
826
uses the ``id`` field as its primary key. The following example constructs
814
- the same query as the preceding code:
827
+ the same query as the preceding code by using the ``id`` field :
815
828
816
829
.. literalinclude:: /includes/interact-data/specify-a-query.py
817
830
:language: python
0 commit comments