@@ -116,6 +116,7 @@ collection:
116
116
:language: python
117
117
118
118
.. output::
119
+ :visible: false
119
120
120
121
<QuerySet [<Movie: The Great Train Robbery>, <Movie: A Corner in Wheat>,
121
122
<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``:
151
152
:language: python
152
153
153
154
.. output::
155
+ :visible: false
154
156
155
157
<QuerySet [<Movie: Wild Palms>, <Movie: Streets of Laredo>,
156
158
<Movie: The Way We Live Now>]>
@@ -186,6 +188,7 @@ value of ``"Finding Nemo"``:
186
188
:language: python
187
189
188
190
.. output::
191
+ :visible: false
189
192
190
193
<Movie: Finding Nemo>
191
194
@@ -212,6 +215,7 @@ The following example calls the ``exclude()`` method on the
212
215
:language: python
213
216
214
217
.. output::
218
+ :visible: false
215
219
216
220
<QuerySet [<Movie: The Iron Horse>, <Movie: Le grand jeu>,
217
221
<Movie: The Devil Is a Woman>, <Movie: Children in the Wind>,
@@ -316,6 +320,7 @@ includes the text ``"coming-of-age"``:
316
320
:language: python
317
321
318
322
.. output::
323
+ :visible: false
319
324
320
325
<QuerySet [<Movie: Murmur of the Heart>, <Movie: Desert Bloom>,
321
326
<Movie: Girls Town>, <Movie: Get Real>, <Movie: Man of Steel>,
@@ -359,10 +364,6 @@ matching criteria for text values:
359
364
- | Matches field values that are less or equal to than the specified
360
365
value.
361
366
362
- * - ``__ne``
363
- - | Matches field values that are not equal
364
- to the specified value.
365
-
366
367
367
368
Example
368
369
```````
@@ -380,6 +381,7 @@ is less than or equal to ``50``:
380
381
:language: python
381
382
382
383
.. output::
384
+ :visible: false
383
385
384
386
<QuerySet [<Movie: The Great Train Robbery>, <Movie: A Corner in Wheat>,
385
387
<Movie: Winsor McCay, the Famous Cartoonist of the N.Y. Herald and His Moving Comics>,
@@ -412,10 +414,11 @@ Example
412
414
413
415
This example uses a lookup that spans the ``Movie`` and ``Award``
414
416
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``:
419
422
420
423
.. io-code-block::
421
424
:copyable:
@@ -426,6 +429,7 @@ documents in which the ``awards.wins`` nested field value is ``93``:
426
429
:language: python
427
430
428
431
.. output::
432
+ :visible: false
429
433
430
434
<QuerySet [<Movie: The Queen>, <Movie: Dallas Buyers Club>]>
431
435
@@ -456,6 +460,7 @@ begins with the text ``"nominated"``, instructing
456
460
:language: python
457
461
458
462
.. output::
463
+ :visible: false
459
464
460
465
<QuerySet [<Movie: Hallelujah>, <Movie: Little Caesar>,
461
466
<Movie: Morocco>, <Movie: Romance>, <Movie: The Guardsman>,
@@ -509,6 +514,7 @@ This example performs the following actions:
509
514
:language: python
510
515
511
516
.. output::
517
+ :visible: false
512
518
513
519
<QuerySet [<Movie: Rocky>, <Movie: Rocky II>, <Movie: Rocky III>,
514
520
<Movie: Rocky IV>, <Movie: Rocky V>, <Movie: Rocky Marciano>,
@@ -556,6 +562,7 @@ This example performs the following actions:
556
562
:language: python
557
563
558
564
.. output::
565
+ :visible: false
559
566
560
567
<QuerySet [<Movie: Inception>, <Movie: Winter's Bone>]>
561
568
@@ -583,21 +590,142 @@ for the first document in which the ``genres`` value is
583
590
:language: python
584
591
585
592
.. output::
593
+ :visible: false
586
594
587
595
<Movie: The Crew>
588
596
589
597
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:
592
608
593
609
Query a JSONField
594
610
~~~~~~~~~~~~~~~~~
595
611
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
+
596
677
Query a Primary Key Field
597
678
~~~~~~~~~~~~~~~~~~~~~~~~~
598
679
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>
601
719
602
720
Additional Information
603
721
----------------------
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