Skip to content

Commit a6d9814

Browse files
authored
DOCSP-46322: Raw queries (#1)
* DOCSP-46322: Raw queries * landing * code edits * jib feedback * managers * fix * mongoclient operations * more mongoclient * fix code * use include * query api section * fix * sample data include edit * fix str
1 parent 4849198 commit a6d9814

File tree

6 files changed

+510
-8
lines changed

6 files changed

+510
-8
lines changed

snooty.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ intersphinx = [ "https://www.mongodb.com/docs/manual/objects.inv",
66
"http://docs.djangoproject.com/en/5.0/_objects/"
77
]
88

9-
# toc_landing_pages = ["/paths/to/pages/that/have/nested/content"]
9+
toc_landing_pages = [
10+
"/interact-data"
11+
]
1012

1113
[constants]
1214
django-odm = "Django MongoDB Backend"
1315
api = "https://django-mongodb.readthedocs.io/en/latest/"
1416
mdb-server = "MongoDB Server"
1517
django-version = "5.0"
1618
django-docs = "https://docs.djangoproject.com/en/{+django-version+}"
19+
pymongo-docs = "https://www.mongodb.com/docs/languages/python/pymongo-driver/current"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# start-models
2+
from django.db import models
3+
from django_mongodb_backend.fields import ArrayField
4+
from django_mongodb_backend.managers import MongoManager
5+
6+
class Movie(models.Model):
7+
title = models.CharField(max_length=200)
8+
plot = models.TextField(blank=True)
9+
runtime = models.IntegerField(default=0)
10+
released = models.DateTimeField("release date", null=True, blank=True)
11+
genres = ArrayField(models.CharField(max_length=100), null=True, blank=True)
12+
objects = MongoManager()
13+
14+
class Meta:
15+
db_table = "movies"
16+
managed = False
17+
18+
def __str__(self):
19+
return self.title
20+
21+
class Theater(models.Model):
22+
theaterId = models.IntegerField(default=0)
23+
objects = MongoManager()
24+
25+
class Meta:
26+
db_table = "theaters"
27+
managed = False
28+
29+
def __str__(self):
30+
return self.theaterId
31+
# end-models
32+
33+
# start-filter-project
34+
movies = Movie.objects.raw_aggregate([
35+
{"$match": {"title": "The Parent Trap"}},
36+
{"$project": {
37+
"title": 1,
38+
"released": 1
39+
}
40+
}])
41+
42+
for m in movies:
43+
print(f"Plot of {m.title}, released on {m.released}: {m.plot}\n")
44+
# end-filter-project
45+
46+
# start-atlas-search
47+
movies = Movie.objects.raw_aggregate([
48+
{
49+
"$search": {
50+
"index": "<search-index-name>",
51+
"phrase": {
52+
"path": "plot",
53+
"query": "whirlwind romance",
54+
"slop": 3
55+
},
56+
"highlight": {
57+
"path": "plot"
58+
}
59+
}
60+
},
61+
{
62+
"$project": {
63+
"title": 1,
64+
"highlight": {"$meta": "searchHighlights"}
65+
}
66+
}
67+
])
68+
69+
for m in movies:
70+
print(f"Title: {m.title}, text match details: {m.highlight}\n")
71+
# end-atlas-search
72+
73+
# start-geo
74+
chicago_bounds = {
75+
"type": "Polygon",
76+
"coordinates": [[
77+
[-87.851, 41.976],
78+
[-87.851, 41.653],
79+
[-87.651, 41.653],
80+
[-87.651, 41.976],
81+
[-87.851, 41.976]
82+
]]
83+
}
84+
85+
theaters = Theater.objects.raw_aggregate([
86+
{
87+
"$match": {
88+
"location.geo": {
89+
"$geoWithin": {
90+
"$geometry": chicago_bounds
91+
}
92+
}
93+
}
94+
},
95+
{
96+
"$project": {
97+
"theaterId": 1
98+
}
99+
}
100+
])
101+
102+
for t in theaters:
103+
print(f"Theater ID: {t.theaterId}")
104+
# end-geo

source/includes/use-sample-data.rst

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
The |model-classes| an inner ``Meta`` class, which specifies
2+
model metadata, and a ``__str__()`` method, which defines the
3+
model's string representation. To learn about these
4+
model features, see :ref:`django-models-define` in the
5+
Create Models guide.
6+
7+
Run Code Examples
8+
`````````````````
9+
10+
You can use the Python interactive shell to run the code examples.
11+
To enter the shell, run the following command from your project's
12+
root directory:
13+
14+
.. code-block:: bash
15+
16+
python manage.py shell
17+
18+
After entering the Python shell, ensure that you import the following models and
19+
modules:
20+
21+
|model-imports|
22+
23+
To learn how to create a {+framework+} application that uses the ``Movie``
24+
model and the Python interactive shell to interact with MongoDB documents,
25+
visit the :ref:`django-get-started` tutorial.

source/index.txt

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ Django MongoDB Backend
1111

1212
.. toctree::
1313

14+
Interact with Data </interact-data>
1415
Issues & Help </issues-and-help>
1516
Compatibility </compatibility>
1617

1718

1819
.. TODO:
1920
Get Started </get-started>
2021
Connection Configuration </connect>
21-
Interact with Data </interact-data>
2222
Model Your Data </model-data>
2323
Django Feature Limitations </feature-limitations>
2424
API Documentation <{+api+}>
@@ -29,6 +29,12 @@ Introduction
2929
Welcome to the documentation site for the official {+django-odm+},
3030
a Django database backend that uses PyMongo to connect to MongoDB.
3131

32+
Interact with Data
33+
------------------
34+
35+
Learn how to use {+django-odm+} to perform operations on MongoDB data
36+
in the :ref:`django-interact-data` section.
37+
3238
.. TODO:
3339

3440
.. Get Started
@@ -43,12 +49,6 @@ a Django database backend that uses PyMongo to connect to MongoDB.
4349
.. Learn how to configure a connection to a MongoDB deployment
4450
in the :ref:`django-connection-configuration` section.
4551

46-
.. Interact with Data
47-
.. ------------------
48-
49-
.. Learn how to use {+django-odm+} to perform operations on MongoDB data
50-
in the :ref:`django-interact-data` section.
51-
5252
.. Model Your Data
5353
.. ---------------
5454

source/interact-data.txt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.. _django-interact-data:
2+
3+
==================
4+
Interact with Data
5+
==================
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: odm, crud, query
13+
14+
.. toctree::
15+
:caption: Interact with Data
16+
17+
Perform Raw Queries </interact-data/raw-queries>

0 commit comments

Comments
 (0)