Skip to content

Commit 53bbe4c

Browse files
committed
rest of page
1 parent b5ceafc commit 53bbe4c

File tree

3 files changed

+162
-15
lines changed

3 files changed

+162
-15
lines changed

snooty.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ intersphinx = [ "https://www.mongodb.com/docs/manual/objects.inv",
1111
django-odm = "Django MongoDB Backend"
1212
django-version = "5.0"
1313
api = "https://django-mongodb.readthedocs.io/en/latest/"
14-
django-docs = "https://docs.djangoproject.com/en/{+django-version+}/ref/"
14+
django-docs = "https://docs.djangoproject.com/en/{+django-version+}"

source/includes/interact-data/specify-a-query.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Movie(models.Model):
1717
released = models.DateTimeField("release date", null=True, blank=True)
1818
awards = EmbeddedModelField(Award)
1919
genres = ArrayField(models.CharField(max_length=100), null=True, blank=True)
20+
imdb = models.JSONField(null=True)
2021

2122
class Meta:
2223
db_table = "movies"
@@ -63,9 +64,27 @@ def __str__(self):
6364
# end-sort
6465

6566
# start-limit
66-
Movie.objects.filter(released=timezone.make_aware(datetime(2010, 7, 1)))[3:6]
67+
Movie.objects.filter(released=timezone.make_aware(datetime(2010, 7, 16)))[2:4]
6768
# end-limit
6869

6970
# start-first
7071
Movie.objects.filter(genres=["Crime", "Comedy"]).first()
71-
# end-first
72+
# end-first
73+
74+
# start-json
75+
Movie.objects.filter(imdb__votes__gt=900000)
76+
# end-json
77+
78+
# start-kt
79+
from django.db.models.fields.json import KT
80+
81+
Movie.objects.annotate(score=KT("imdb__rating")).all().order_by("-score")
82+
# end-kt
83+
84+
# start-primary-key-pk
85+
Movie.objects.get(pk=ObjectId("573a1394f29313caabce0d37"))
86+
# end-primary-key-pk
87+
88+
# start-primary-key-id
89+
Movie.objects.get(id=ObjectId("573a1394f29313caabce0d37"))
90+
# end-primary-key-id

source/interact-data/specify-a-query.txt

Lines changed: 140 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ collection:
116116
:language: python
117117

118118
.. output::
119+
:visible: false
119120

120121
<QuerySet [<Movie: The Great Train Robbery>, <Movie: A Corner in Wheat>,
121122
<Movie: Winsor McCay, the Famous Cartoonist of the N.Y. Herald and His Moving Comics>,
@@ -151,6 +152,7 @@ for documents that have a ``runtime`` value of ``300``:
151152
:language: python
152153

153154
.. output::
155+
:visible: false
154156

155157
<QuerySet [<Movie: Wild Palms>, <Movie: Streets of Laredo>,
156158
<Movie: The Way We Live Now>]>
@@ -186,6 +188,7 @@ value of ``"Finding Nemo"``:
186188
:language: python
187189

188190
.. output::
191+
:visible: false
189192

190193
<Movie: Finding Nemo>
191194

@@ -212,6 +215,7 @@ The following example calls the ``exclude()`` method on the
212215
:language: python
213216

214217
.. output::
218+
:visible: false
215219

216220
<QuerySet [<Movie: The Iron Horse>, <Movie: Le grand jeu>,
217221
<Movie: The Devil Is a Woman>, <Movie: Children in the Wind>,
@@ -316,6 +320,7 @@ includes the text ``"coming-of-age"``:
316320
:language: python
317321

318322
.. output::
323+
:visible: false
319324

320325
<QuerySet [<Movie: Murmur of the Heart>, <Movie: Desert Bloom>,
321326
<Movie: Girls Town>, <Movie: Get Real>, <Movie: Man of Steel>,
@@ -359,10 +364,6 @@ matching criteria for text values:
359364
- | Matches field values that are less or equal to than the specified
360365
value.
361366

362-
* - ``__ne``
363-
- | Matches field values that are not equal
364-
to the specified value.
365-
366367

367368
Example
368369
```````
@@ -380,6 +381,7 @@ is less than or equal to ``50``:
380381
:language: python
381382

382383
.. output::
384+
:visible: false
383385

384386
<QuerySet [<Movie: The Great Train Robbery>, <Movie: A Corner in Wheat>,
385387
<Movie: Winsor McCay, the Famous Cartoonist of the N.Y. Herald and His Moving Comics>,
@@ -412,10 +414,11 @@ Example
412414

413415
This example uses a lookup that spans the ``Movie`` and ``Award``
414416
relationship. The ``Movie`` model's ``awards`` field has an
415-
embedded ``Award`` model value, which represents the ``awards``
416-
object field in the MongoDB collection and its nested ``wins``,
417-
``nominations``, and ``text`` fields. The code uses a lookup to query for
418-
documents in which the ``awards.wins`` nested field value is ``93``:
417+
embedded ``Award`` model value. This embedded model represents
418+
the ``awards`` object field in the ``sample_mflix.movies`` collection
419+
and its nested ``wins``, ``nominations``, and ``text`` fields. The
420+
following code uses a lookup to query for documents in which the ``awards.wins``
421+
nested field value is ``93``:
419422

420423
.. io-code-block::
421424
:copyable:
@@ -426,6 +429,7 @@ documents in which the ``awards.wins`` nested field value is ``93``:
426429
:language: python
427430

428431
.. output::
432+
:visible: false
429433

430434
<QuerySet [<Movie: The Queen>, <Movie: Dallas Buyers Club>]>
431435

@@ -456,6 +460,7 @@ begins with the text ``"nominated"``, instructing
456460
:language: python
457461

458462
.. output::
463+
:visible: false
459464

460465
<QuerySet [<Movie: Hallelujah>, <Movie: Little Caesar>,
461466
<Movie: Morocco>, <Movie: Romance>, <Movie: The Guardsman>,
@@ -509,6 +514,7 @@ This example performs the following actions:
509514
:language: python
510515

511516
.. output::
517+
:visible: false
512518

513519
<QuerySet [<Movie: Rocky>, <Movie: Rocky II>, <Movie: Rocky III>,
514520
<Movie: Rocky IV>, <Movie: Rocky V>, <Movie: Rocky Marciano>,
@@ -556,6 +562,7 @@ This example performs the following actions:
556562
:language: python
557563

558564
.. output::
565+
:visible: false
559566

560567
<QuerySet [<Movie: Inception>, <Movie: Winter's Bone>]>
561568

@@ -583,21 +590,142 @@ for the first document in which the ``genres`` value is
583590
:language: python
584591

585592
.. output::
593+
:visible: false
586594

587595
<Movie: The Crew>
588596

589597

590-
Advanced Queries
591-
----------------
598+
Advanced Field Queries
599+
----------------------
600+
601+
This section describes how to run queries on the
602+
following field types:
603+
604+
- :ref:`JSONField <django-query-jsonfield>`
605+
- :ref:`Primary Key <django-query-primary-key>`
606+
607+
.. _django-query-jsonfield:
592608

593609
Query a JSONField
594610
~~~~~~~~~~~~~~~~~
595611

612+
You can query data stored in a ``JSONField`` value by using the
613+
same syntax as show in the :ref:`django-query-span-relationships`
614+
section. Chain each field and nested field name together, separated
615+
by double underscores (``__``), until you reach the field you want
616+
to query.
617+
618+
The following example queries the ``imdb`` field, a ``JSONField``,
619+
for values of its nested ``votes`` field that exceed ``900000``:
620+
621+
.. io-code-block::
622+
:copyable:
623+
624+
.. input:: /includes/interact-data/specify-a-query.py
625+
:start-after: start-json
626+
:end-before: end-json
627+
:language: python
628+
629+
.. output::
630+
:visible: false
631+
632+
<QuerySet [<Movie: La nao capitana>, <Movie: The Godfather>,
633+
<Movie: This Is Spinal Tap>, <Movie: Forrest Gump>, <Movie: Pulp Fiction>,
634+
<Movie: The Shawshank Redemption>, <Movie: Se7en>,
635+
<Movie: The Lord of the Rings: The Fellowship of the Ring>,
636+
<Movie: The Matrix>, <Movie: Fight Club>,
637+
<Movie: The Lord of the Rings: The Return of the King>,
638+
<Movie: The Lord of the Rings: The Two Towers>, <Movie: The Shawshank Redemption>,
639+
<Movie: Landet som icke èr>, <Movie: The Dark Knight>,
640+
<Movie: The Danish Girl>, <Movie: The Dark Knight Rises>, <Movie: Inception>,
641+
<Movie: Catching the Sun>, <Movie: Scouts Guide to the Zombie Apocalypse>,
642+
'...(remaining elements truncated)...']>
643+
644+
JSONField Transforms
645+
````````````````````
646+
647+
You can use the ``KT()`` method to specify a key, index, or path
648+
transform of a ``JSONField``. For example, the following code uses
649+
the ``annotate()`` and ``KT()`` methods to create a new ``score`` key, which
650+
stores ``imdb.rating`` nested field values. Then, the code sorts
651+
each document in the ``sample_mflix.movies`` collection by
652+
their descending ``score`` values:
653+
654+
.. io-code-block::
655+
:copyable:
656+
657+
.. input:: /includes/interact-data/specify-a-query.py
658+
:start-after: start-kt
659+
:end-before: end-kt
660+
:language: python
661+
662+
.. output::
663+
:visible: false
664+
665+
<QuerySet [<Movie: No Tomorrow>, <Movie: The Deposit>, <Movie: Man Down>,
666+
<Movie: Convenience>, <Movie: Scouts Guide to the Zombie Apocalypse>,
667+
<Movie: Another World>, <Movie: The Danish Girl>, <Movie: Ad Inexplorata>,
668+
<Movie: Landet som icke èr>, <Movie: The Ghost and the Whale>,
669+
<Movie: Coming to Terms>, <Movie: La nao capitana>, <Movie: All Eyes and Ears>,
670+
<Movie: Catching the Sun>, <Movie: Manhattan Romance>, <Movie: Anomalisa>,
671+
<Movie: Outliving Emily>, <Movie: Mary Loss of Soul>,
672+
<Movie: The Childhood of a Leader>, <Movie: Krot na more>,
673+
'...(remaining elements truncated)...']>
674+
675+
.. _django-query-primary-key:
676+
596677
Query a Primary Key Field
597678
~~~~~~~~~~~~~~~~~~~~~~~~~
598679

599-
Run a Destructive Operation
600-
~~~~~~~~~~~~~~~~~~~~~~~~~~~
680+
You can use the ``pk`` lookup shortcut to query primary key
681+
values, which MongoDB stores as ``ObjectId`` values.
682+
683+
The following example queries the ``sample_mflix.movies`` collection
684+
for a document whose primary key is ``ObjectId("573a1394f29313caabce0d37")``:
685+
686+
.. io-code-block::
687+
:copyable:
688+
689+
.. input:: /includes/interact-data/specify-a-query.py
690+
:start-after: start-primary-key-pk
691+
:end-before: end-primary-keypk
692+
:language: python
693+
694+
.. output::
695+
:visible: false
696+
697+
// Your ObjectId values might differ
698+
699+
<Movie: Vertigo>
700+
701+
The ``Movie`` model, which represents the ``sample_mflix.movies`` collection,
702+
uses the ``id`` field as its primary key. The following example constructs
703+
the same query as the preceding code:
704+
705+
.. io-code-block::
706+
:copyable:
707+
708+
.. input:: /includes/interact-data/specify-a-query.py
709+
:start-after: start-primary-key-id
710+
:end-before: end-primary-key-id
711+
:language: python
712+
713+
.. output::
714+
:visible: false
715+
716+
// Your ObjectId values might differ
717+
718+
<Movie: Vertigo>
601719

602720
Additional Information
603721
----------------------
722+
723+
To learn how to run raw database queries using MongoDB's aggregation
724+
pipeline syntax, rather than ``QuerySet`` API functions, see the
725+
:ref:`django-raw-queries` guide.
726+
727+
To learn how to perform other ``QuerySet`` operations, see the
728+
:ref:`django-crud` guide.
729+
730+
To learn more about Django queries, see `Making queries <{+django-docs+}/topics/db/queries>`__
731+
in the Django documentation.

0 commit comments

Comments
 (0)