@@ -23,29 +23,32 @@ Overview
2323In this guide, you can learn how to use {+django-odm+} to specify
2424a database query.
2525
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+
2636The Django ``QuerySet`` API provides methods that allow you to retrieve
2737model 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
2939operations and allows you to interact with your MongoDB data by referencing
3040Django models. By default, Django adds a ``Manager`` named ``objects``
3141to every model class.
3242
3343When you assign a ``QuerySet`` to a variable, {+django-odm+} does not
3444perform the operation until you evaluate the variable. After
3545evaluating 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.
3747
3848.. tip::
3949
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.
4952
5053Sample Data
5154~~~~~~~~~~~
@@ -86,7 +89,7 @@ Run a Query
8689-----------
8790
8891To 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
9093query filter.
9194
9295This section describes how to use the following methods from
@@ -104,7 +107,7 @@ Retrieve All Documents
104107~~~~~~~~~~~~~~~~~~~~~~
105108
106109To retrieve all documents from a collection, call the ``all()``
107- method on your model's ``Manager`` class .
110+ method on your model's manager .
108111
109112The following example calls the ``all()`` method to retrieve
110113all documents in the ``sample_mflix.movies`` collection:
@@ -137,7 +140,7 @@ Retrieve Matching Documents
137140~~~~~~~~~~~~~~~~~~~~~~~~~~~
138141
139142To 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 .
141144Pass a query filter to the ``filter()`` method that specifies your
142145query criteria.
143146
@@ -165,9 +168,8 @@ Retrieve One Document
165168~~~~~~~~~~~~~~~~~~~~~
166169
167170To 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.
171173
172174.. important::
173175
@@ -176,7 +178,6 @@ query criteria.
176178 that might match multiple, use the :ref:`first() <django-query-first>`
177179 method.
178180
179-
180181The following example calls the ``get()`` method
181182to retrieve one document that has a ``title``
182183value of ``"Finding Nemo"``:
@@ -242,13 +243,15 @@ Run Raw Database Queries
242243~~~~~~~~~~~~~~~~~~~~~~~~
243244
244245If 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
246247method allows you to specify your query criteria in a MongoDB
247248aggregation pipeline, which you pass as an argument to
248249``raw_aggregate()``.
249250
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:
252255
253256Customize Your Query Filter
254257---------------------------
@@ -271,7 +274,7 @@ expression matching, and year value matching for datetime fields.
271274
272275 To view a full list of lookup types, see `Field lookups
273276 <{+django-docs+}/ref/models/querysets/#field-lookups>`__ in the
274- ``QuerySet`` Django documentation .
277+ ``QuerySet`` Django API reference .
275278
276279This section describes how to refine your query filters
277280in the following ways:
@@ -296,33 +299,33 @@ matching criteria for text values:
296299 * - Lookup Type
297300 - Description
298301
299- * - ``__exact ``
302+ * - ``exact ``
300303 - | Specifies an exact text match. If you do not provide a
301304 lookup type in your query filter, {+django-odm+} uses this
302305 type by default.
303306
304307 | You can specify a case-insensitive exact text match
305- by using ``__iexact ``.
306- * - ``__contains ``
308+ by using ``iexact ``.
309+ * - ``contains ``
307310 - | Specifies a partial text match.
308311
309312 | You can specify a case-insensitive partial text match
310- by using ``__icontains ``.
311- * - ``__startswith ``
313+ by using ``icontains ``.
314+ * - ``startswith ``
312315 - | Matches field values that begin with the specified text.
313316
314317 | You can specify a case-insensitive partial text match
315- by using ``__istartswith ``.
316- * - ``__endswith ``
318+ by using ``istartswith ``.
319+ * - ``endswith ``
317320 - | Matches field values that end with the specified text.
318321
319322 | You can specify a case-insensitive partial text match
320- by using ``__iendswith ``.
323+ by using ``iendswith ``.
321324
322325Example
323326```````
324327
325- The following example uses the ``__contains `` lookup to
328+ The following example uses the ``contains `` lookup to
326329query for documents in which the ``plot`` value
327330includes the text ``"coming-of-age"``:
328331
@@ -363,27 +366,27 @@ against a specified value:
363366 * - Lookup Type
364367 - Description
365368
366- * - ``__gt ``
369+ * - ``gt ``
367370 - | Matches field values that are greater than the
368371 specified value.
369372
370- * - ``__gte ``
373+ * - ``gte ``
371374 - | Matches field values that are greater than or equal
372375 to the specified value.
373376
374- * - ``__lt ``
377+ * - ``lt ``
375378 - | Matches field values that are less than the specified
376379 value.
377380
378- * - ``__lte ``
381+ * - ``lte ``
379382 - | Matches field values that are less or equal to than the specified
380383 value.
381384
382385
383386Example
384387```````
385388
386- The following example uses the ``__lte `` lookup to
389+ The following example uses the ``lte `` lookup to
387390query for documents in which the ``runtime`` value
388391is less than or equal to ``50``:
389392
@@ -488,12 +491,15 @@ You can provide a sort order for your query results by using the
488491of a given field, pass the field name as an argument. To specify a descending
489492sort, pass the field name prefixed with a minus symbol (``-``) as an argument.
490493
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+
491497Example
492498```````
493499
494500This example performs the following actions:
495501
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
497503 the ``sample_mflix.movies`` collection
498504- Queries documents that have a ``title`` value starting
499505 with the text ``"Rocky"``
@@ -517,8 +523,8 @@ This example performs the following actions:
517523
518524.. _django-query-limit:
519525
520- Limit Results
521- ~~~~~~~~~~~~~
526+ Skip and Limit Results
527+ ~~~~~~~~~~~~~~~~~~~~~~
522528
523529You can specify the number of results that a query returns
524530by using Python's array-slicing syntax, as shown
@@ -531,7 +537,7 @@ in the following code:
531537
532538Replace the ``<start>`` and ``<end>`` placeholders with integer
533539values 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.
535541
536542If you omit the ``<start>`` value, the query returns each result,
537543beginning 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:
618624
619625 Model.objects.filter(<model field>__<embedded model field>=<value>)
620626
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+
621631Example
622632```````
623633
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
628637and 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 ``:
631640
632641.. io-code-block::
633642 :copyable:
@@ -640,17 +649,21 @@ nested field value is ``93``:
640649 .. output::
641650 :visible: false
642651
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)>]>
644657
645658.. _django-query-arrayfield:
646659
647660Query an ArrayField
648- ~~~~~~~~~~~~~~~~~
661+ ~~~~~~~~~~~~~~~~~~~
649662
650663You can query data stored in an ``ArrayField`` value
651664by using the standard query syntax. {+django-odm+} provides
652665additional field lookup types for querying ``ArrayField`` values,
653- as described in the following table:
666+ which are described in the following table:
654667
655668.. list-table::
656669 :header-rows: 1
@@ -659,27 +672,27 @@ as described in the following table:
659672 * - Lookup Type
660673 - Description
661674
662- * - ``__contains ``
675+ * - ``contains ``
663676 - | Matches fields that store the provided value as
664677 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`
666679 section of this guide.
667680
668- * - ``__contained_by ``
681+ * - ``contained_by ``
669682 - | 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 ``.
671684
672- * - ``__overlap ``
685+ * - ``overlap ``
673686 - | Matches field that store any of the provided values.
674687
675- * - ``__len ``
688+ * - ``len ``
676689 - | Matches fields that store the number of values provided.
677690
678691Example
679692```````
680693
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
683696``["Adventure", "Family"]`` array:
684697
685698.. io-code-block::
@@ -713,7 +726,7 @@ section. Chain each field and nested field name together, separated
713726by double underscores (``__``), until you reach the field you want
714727to query.
715728
716- The following example queries the ``imdb`` field, a ``JSONField``,
729+ The following example queries the ``JSONField``-valued ``imdb`` field
717730for values of its nested ``votes`` field that exceed ``900000``:
718731
719732.. io-code-block::
@@ -811,7 +824,7 @@ for a document whose primary key is ``ObjectId("573a1394f29313caabce0d37")``:
811824
812825The ``Movie`` model, which represents the ``sample_mflix.movies`` collection,
813826uses 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 :
815828
816829.. literalinclude:: /includes/interact-data/specify-a-query.py
817830 :language: python
0 commit comments