MongoEngine – Indexes

MongoEngine – Indexes

An indexed collection results in faster processing of queries. By default, every collection is automatically indexed on _id field. In addition, you can create index on one or more fields.
While using MongoEngine, indexes are created by specifying ‘indexes’ key in meta dictionary of definition of Document class.
Value of indexes property is a list of fields. In the following example, we ask documents in student collection be indexed according to name field.

from mongoengine import connect
from mongoengine import StringField, IntField, Document, DecimalField, QuerySet
connect('mydata', host='mongodb://localhost/mydata')
class MyQuerySet(QuerySet):
def myqrymethod(self):
return self.filter(book_name__endswith='hm')
class Books(Document):
book_id = IntField(unique=True, required=True)
book_name = StringField(max_length=50)
book_price = DecimalField()
meta = {'indexes': ['book_name']}
def _init__(self, book_id, book_name, book_price):
self.book_id = book_id
self.book_name = book_name
self.book_price = book_price
b1 = Books(book_id=1008, book_name='Software Engineering',
book_price=2000)
b1.save()
b2 = Books(book_id=1009, book_name='Theory of Automata',
book_price=2100)
b2.save()

By default, indexing order is ascending. Order may be specified by prepending ‘+’ for ascending or ‘-‘ for descending order.
To create compound index, use a tuple of field names, optionally having + or – symbol attached to indicate sort order.
In the following example,Books document class contains definition of compound index on book_name (note - symbol prefixed to course field which means index is built namewise ascending and coursewise descending order. )

from mongoengine import connect
from mongoengine import StringField, IntField, Document, DecimalField, QuerySet
connect('mydata', host='mongodb://localhost/mydata')
class MyQuerySet(QuerySet):
def myqrymethod(self):
return self.filter(book_name__endswith='hm')
class Books(Document):
book_id = IntField(unique=True, required=True)
book_name = StringField(max_length=50)
book_price = DecimalField()
meta = {'indexes': ['+book_name']}
def _init__(self, book_id, book_name, book_price):
self.book_id = book_id
self.book_name = book_name
self.book_price = book_price
b1 = Books(book_id=1008, book_name='Software Engineering',
book_price=2000)
b1.save()
b2 = Books(book_id=1009, book_name='Theory of Automata',
book_price=2100)
b2.save()

MongoDB Compass will show indexes as below −
Value of ‘indexes’ may be a dictionary of various options as below −

fields The fields to index.
cls If allow_inheritance is turned on, you can configure whether the index should have the _cls field added automatically.
sparse Whether the index should be sparse.
unique Whether the index should be unique.
expireAfterSeconds automatically expire data from a collection by setting the time in seconds
name Allows you to specify a name for the index
collation Allows to create case insensitive indexes

Following example creates index on name field that expires after 3600 seconds.
```python
from mongoengine import *
con=connect('mydata')
class Books(Document):
book_id = IntField(unique=True, required=True)
book_name = StringField(max_length=50)
book_price = DecimalField()
meta = {'indexes':[{
'fields': ['book_name'],
'expireAfterSeconds': 3600
}]}
```
To specify text index, prefix field name with ‘$’ sign and for hashed index, use ‘#’ as prefix.
Indexes so specified are created automatically as documents are added in the collection. To disable automatic creation, set ‘auto_create_index’ to False in meta attribute.
We have list_indexes() method with Document class that displays list of available indexes.
```python
print(Books.list_indexes())
#Output
[[('book_name', 1)], [('book_id', 1)], [('_id', 1)]]
```
To create index on a field not in the meta dictionary, use create_index() method. The following code will create index on course field −
```python
class Books(Document):
# meta = {'queryset_class': MyQuerySet}
book_id = IntField(unique=True, required=True)
book_name = StringField(max_length=50)
book_price = DecimalField()
meta = {'indexes': [{
'fields': ['book_name'],
'expireAfterSeconds': 3600
}]}
Books.create_index['book_price']
```

MongoEngine – QuerySet Methods (Prev Lesson)
(Next Lesson) MongoEngine – Advanced Queries