Skip to content

Commit 5173440

Browse files
committed
Fixed #74: Documented the new 'indexes' MongoMeta option.
1 parent 95c8e7b commit 5173440

File tree

1 file changed

+29
-51
lines changed

1 file changed

+29
-51
lines changed

docs/source/reference/model-options.rst

Lines changed: 29 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,63 +7,41 @@ various options specific to MongoDB through a special ``class MongoMeta``. ::
77
class FooModel(models.Model):
88
...
99
class MongoMeta:
10-
# mongo options here
10+
# Mongo options here
1111
...
1212

1313
Indexes
1414
-------
15-
Make use of MongoDB's wide variety of indexes.
16-
17-
``index_together``
18-
**Form 1**: A tuple of field names or :samp:`({field name}, {index direction})`
19-
tuples to index together. For instance, ::
20-
15+
Django MongoDB Engine already understands the standard
16+
:attr:`~django.db.models.Field.db_index` and
17+
:attr:`~django.db.models.Options.unique_together` options and generates the
18+
corresponding MongoDB indexes on ``syncdb``.
19+
20+
To make use of other index features, like multi-key indexes and Geospatial
21+
Indexing, additional indexes can be specified using the ``indexes`` setting. ::
22+
23+
class Club(models.Model):
24+
location = ListField()
25+
rating = models.FloatField()
26+
admission = models.IntegerField()
27+
...
2128
class MongoMeta:
22-
index_together = ['name', ('last_name', pymongo.DESCENDING)]
23-
24-
results in this call::
25-
26-
collection.ensure_index([('name', 1), ('last_name', -1)])
27-
28-
(``pymongo.DESCENDING`` being the same as -1)
29-
30-
**Form 2**: A list of dictionaries containing an item with the key *field*
31-
and a list of field names or :samp:`({field name}, {index direction})` tuples
32-
to index together as value, optionally containing keyword arguments to pass to
33-
:meth:`pymongo.Collection.ensure_index`. For example, ::
34-
35-
index_together = [{'fields' : ['name', ('last_name', pymongo.DESCENDING)],
36-
'name' : 'name-lastname-index'}]
37-
38-
results in this call::
39-
40-
collection.ensure_index([('name', 1), ('last_name', -1)], name='name-lastname-index')
41-
42-
``descending_indexes``
43-
A list of fields whose index shall be descending rather than ascending.
44-
For example, ::
45-
46-
class FooModel(models.Model):
47-
a = models.CharField(db_index=True)
48-
b = models.CharField(db_index=True)
49-
50-
class MongoMeta:
51-
descending_indexes = ['b']
52-
53-
would create an ascending index on field ``a`` and a descending one on ``b``.
54-
55-
``sparse_indexes``
56-
A list of field names or tuples of field names whose index should be sparse_.
57-
This example defines a sparse index on `a` and a sparse index on `b, c`::
58-
59-
class FooModel(models.Model):
60-
a = models.IntegerField(null=True)
61-
b = models.IntegerField(null=True)
62-
c = models.IntegerField(null=True)
29+
indexes = [
30+
[('rating', -1)],
31+
[('rating', -1), ('admission', 1)],
32+
{'fields': [('location', '2d')], 'min': -42, 'max': 42},
33+
]
34+
35+
``indexes`` can be specified in two ways:
36+
37+
* The simple "without options" form is a list of ``(field, direction)`` pairs.
38+
For example, a single ascending index (the same thing you get using ``db_index``)
39+
is expressed as ``[(field, 1)]``. A multi-key, descending index can be written
40+
as ``[(field1, -1), (field2, -1), ...]``.
41+
* The second form is slightly more verbose but takes additional MongoDB index
42+
options. A descending, sparse index for instance may be expressed as
43+
``{'fields': [(field, -1)], 'sparse': True}``.
6344

64-
class MongoMeta:
65-
index_together = ('b', 'c')
66-
sparse_indexes = ['a', ('b', 'c')]
6745

6846
Capped Collections
6947
------------------

0 commit comments

Comments
 (0)