Skip to content

Commit 1c91a13

Browse files
committed
Merge remote-tracking branch 'upstream/master' into DOCSP-43200-django-get-started
2 parents ca9c109 + a6d9814 commit 1c91a13

File tree

6 files changed

+513
-12
lines changed

6 files changed

+513
-12
lines changed

snooty.toml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ name = "django"
22
title = "Django MongoDB Backend"
33

44
intersphinx = [ "https://www.mongodb.com/docs/manual/objects.inv",
5-
"https://www.mongodb.com/docs/atlas/objects.inv"
5+
"https://www.mongodb.com/docs/atlas/objects.inv",
6+
"http://docs.djangoproject.com/en/5.0/_objects/"
67
]
78

89
toc_landing_pages = [
9-
"/get-started"
10+
"/get-started",
11+
"/interact-data",
1012
]
1113

1214
[constants]
@@ -16,4 +18,5 @@ mdb-server = "MongoDB Server"
1618
django-version = "5.0"
1719
django-docs = "https://docs.djangoproject.com/en/{+django-version+}"
1820
django-api = "https://django-mongodb-backend.readthedocs.io/en/latest/"
19-
pymongo-version = "4.10"
21+
pymongo-version = "4.10"
22+
pymongo-docs = "https://www.mongodb.com/docs/languages/python/pymongo-driver/current"
Lines changed: 104 additions & 0 deletions
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

Lines changed: 25 additions & 0 deletions
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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ Django MongoDB Backend
1212
.. toctree::
1313

1414
Get Started </get-started>
15+
Interact with Data </interact-data>
1516
Issues & Help </issues-and-help>
1617
Compatibility </compatibility>
1718

18-
1919
.. TODO:
2020
Connection Configuration </connect>
21-
Interact with Data </interact-data>
2221
Model Your Data </model-data>
2322
Django Feature Limitations </feature-limitations>
2423
API Documentation <{+api+}>
@@ -31,10 +30,16 @@ a Django database backend that uses PyMongo to connect to MongoDB.
3130

3231
Get Started
3332
-----------
34-
33+
3534
Learn how to install {+django-odm+}, establish a connection to MongoDB, and begin
3635
working with data in the :ref:`django-get-started` tutorial.
3736

37+
Interact with Data
38+
------------------
39+
40+
Learn how to use {+django-odm+} to perform operations on MongoDB data
41+
in the :ref:`django-interact-data` section.
42+
3843
.. TODO:
3944

4045
.. Connect to MongoDB
@@ -43,12 +48,6 @@ working with data in the :ref:`django-get-started` tutorial.
4348
.. Learn how to configure a connection to a MongoDB deployment
4449
in the :ref:`django-connection-configuration` section.
4550

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-
5251
.. Model Your Data
5352
.. ---------------
5453

source/interact-data.txt

Lines changed: 17 additions & 0 deletions
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)