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.
Using Compass, we can build index very easily. Click on CREATE INDEX button on Indexes tab as shown in figure below−
A dialog box appears as shown. Choose name of index, field on which to index, order of index (ascending or descending) and other options.
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 * con=connect(''mydata'') class student(Document): name=StringField(required=True) course=StringField() meta = {''indexes'':[''name'']} s1=student() s1.name=''Avinash'' s1.course=''DataScience'' s1.save() s2=student() s2.name=''Anita'' s2.course=''WebDesign'' s2.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, student document class contains definition of compound index on name and course (note – symbol prefixed to course field which means index is built namewise ascending and coursewise descending order.
from mongoengine import * con=connect(''mydata'') class student(Document): name=StringField(required=True) course=StringField() meta = {''indexes'':[(''name'',''-course'')]} s1=student() s1.name=''Avinash'' s1.course=''DataScience'' s1.save() s2=student() s2.name=''Anita'' s2.course=''WebDesign'' s2.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.
from mongoengine import * con=connect(''mydata'') class student(Document): name=StringField(required=True) course=StringField() meta = {''indexes'':[{ ''fields'': [''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.
print (student.list_indexes()) [[(''name'', 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 −
class student(Document): name=StringField(required=True) course=StringField() meta = {''indexes'':[{ ''fields'': [''name''], ''expireAfterSeconds'': 3600 } ]} student.create_index([''course''])
”;