Skip to content

Commit ea6b1b3

Browse files
committed
fixes
1 parent 4e6ee50 commit ea6b1b3

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

source/includes/model-data/indexes.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
class Nutrition(models.Model):
77
calories = models.IntegerField(default=0)
8-
fat_grams = models.IntegerField(default=0)
98
carb_grams = models.IntegerField(default=0)
109
protein_grams = models.IntegerField(default=0)
1110

@@ -71,7 +70,7 @@ class Meta:
7170
# end-partial
7271

7372
# start-unique-single
74-
title = models.CharField(max_length=200, unique=True)
73+
cuisine = models.CharField(max_length=200, unique=True)
7574
# end-unique-single
7675

7776
# start-unique-compound
@@ -81,14 +80,16 @@ class Meta:
8180
models.Index(fields=["title", "cuisine"]),
8281
]
8382
constraints = [
84-
models.UniqueConstraint(fields=["title", "cuisine"], name="unique_regional_meal")
83+
models.UniqueConstraint(fields=["title", "cuisine"],
84+
name="unique_regional_meal"),
8585
]
8686
# end-unique-compound
8787

88-
# start-multikey
88+
# start-expression
8989
class Meta:
9090
db_table = "recipes"
9191
indexes = [
92-
models.Index(F("cook_time") + F("prep_time"), name="total_time_idx"),
92+
models.Index(F("cook_time") + F("prep_time"),
93+
name="total_time_idx"),
9394
]
94-
# end-multikey
95+
# end-expression

source/model-data/indexes.txt

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ Overview
2222

2323
In this guide, you can learn how to create MongoDB **indexes** by using your
2424
Django models. Indexes can improve the efficiency of queries and add
25-
additional functionality to querying and storing documents.
25+
additional query and document storage functionality.
2626

2727
Without indexes, MongoDB must scan every document in a collection to find the
2828
documents that match each query. These collection scans are
2929
slow and can negatively affect the performance of your application. However, if an
3030
appropriate index exists for a query, MongoDB can use the index to limit the
31-
documents it must inspect.
31+
documents it inspects.
3232

3333
Django provides the ``Index`` class, which you can use to create an
3434
index on your model. {+django-odm+} creates the same index on your
@@ -47,7 +47,7 @@ The examples in this guide use the ``Recipe`` model, which contains an
4747
embedded ``Nutrition`` model as the value of its ``nutrition`` field.
4848
These model classes have the following definitions:
4949

50-
.. literalinclude:: /includes/interact-data/specify-a-query.py
50+
.. literalinclude:: /includes/model-data/indexes.py
5151
:start-after: start-models
5252
:end-before: end-models
5353
:language: python
@@ -76,11 +76,11 @@ code:
7676
# add more indexes here
7777
]
7878

79-
To define your index, pass the following arguments to the ``Index()`` method:
79+
To define your index, pass the following arguments to the ``models.Index()`` method:
8080

8181
- ``expressions``: Specifies a database expression to index. If you don't pass
8282
the ``fields`` argument, this argument is required. To learn more about this
83-
argument, see the :ref:`django-indexes-expressions` section of this guide.
83+
argument, see the :ref:`django-indexes-functions` section of this guide.
8484

8585
- ``fields``: Specifies a list of fields to index. If you don't pass the
8686
``expressions`` argument, this argument is required.
@@ -189,8 +189,8 @@ an index on the ``nutrition`` embedded model field, which {+django-odm+} creates
189189
on the ``recipes`` collection:
190190

191191
.. literalinclude:: /includes/model-data/indexes.py
192-
:start-after: start-multikey
193-
:end-before: end-multikey
192+
:start-after: start-embedded
193+
:end-before: end-embedded
194194
:language: python
195195
:copyable:
196196
:emphasize-lines: 3-5
@@ -211,7 +211,7 @@ Partial Indexes
211211
~~~~~~~~~~~~~~~
212212

213213
Partial indexes index only the documents in a collection that meet specified
214-
filter condition, which reduces storage use and performance costs.
214+
filter criteria, which reduces storage use and performance costs.
215215

216216
To create a partial index, pass the ``condition`` argument to the ``models.Index()``
217217
method. Set the condition value to a ``Q`` object that includes the filter
@@ -233,7 +233,7 @@ only index documents that have a ``cook_time`` value less than ``30``:
233233
:end-before: end-partial
234234
:language: python
235235
:copyable:
236-
:emphasize-lines: 3-5
236+
:emphasize-lines: 3-7
237237

238238
.. _django-indexes-unique:
239239

@@ -275,6 +275,7 @@ creates a unique compound index on these fields:
275275
:end-before: end-unique-compound
276276
:language: python
277277
:copyable:
278+
:emphasize-lines: 6-8
278279

279280
.. tip::
280281

@@ -295,11 +296,11 @@ The following example updates the ``Recipe`` model's ``Meta`` class to create
295296
an index on the sum of the ``cook_time`` and ``prep_time`` values:
296297

297298
.. literalinclude:: /includes/model-data/indexes.py
298-
:start-after: start-partial
299-
:end-before: end-partial
299+
:start-after: start-expression
300+
:end-before: end-expression
300301
:language: python
301302
:copyable:
302-
:emphasize-lines: 3-5
303+
:emphasize-lines: 3-7
303304

304305
.. tip::
305306

@@ -315,7 +316,7 @@ To learn more about the index types mentioned in this guide,
315316
see the following {+mdb-server+} manual resources:
316317

317318
- :manual:`Single Field Indexes </core/index-single/>`
318-
- :manual:`Compound Indexes </core/index-single/>`
319+
- :manual:`Compound Indexes </core/index-compound/>`
319320
- :manual:`Multikey Indexes </core/index-multikey/>`
320321
- :manual:`Embedded Document Indexes </core/index-single/create-embedded-object-index/>`
321322
- :manual:`Partial Indexes </core/index-partial/>`

0 commit comments

Comments
 (0)