Indexes¶
Intro¶
Indexing is very important for a database. Indexes support the efficient execution of queries in MongoDB. Indexing itself is a very broad topic. Here we won't be discussing how indexes work and what the benefits are.
In this chapter, we will discuss how we can manage indexes using MongoDB-ODM.
Please visit the Indexes section in MongoDB Documentation to learn more about how indexes work in MongoDB.
Define indexes in MongoDB-ODM¶
To create indexes we will define the indexes field in the ODMConfig class.
We declare the indexes field with the class IndexModel that is imported from mongodb_odm. But IndexModel is directly imported from PyMongo. Visit the PyMongo create_indexes doc for details.
import os
from typing import Optional
from mongodb_odm import ASCENDING, Document, IndexModel, apply_indexes, connect
class Player(Document):
name: str
country_code: str
rating: Optional[int] = None
class ODMConfig(Document.ODMConfig):
indexes = [
IndexModel([("contact_email", ASCENDING)]),
]
# Code omitted below
Full file preview
import os
from typing import Optional
from mongodb_odm import ASCENDING, Document, IndexModel, apply_indexes, connect
class Player(Document):
name: str
country_code: str
rating: Optional[int] = None
class ODMConfig(Document.ODMConfig):
indexes = [
IndexModel([("contact_email", ASCENDING)]),
]
def create_documents():
Player(name="Pelé", country_code="BRA").create()
Player(name="Diego Maradona", country_code="ARG", rating=97).create()
def main():
connect(os.environ.get("MONGO_URL", "mongodb://localhost:27017/testdb"))
apply_indexes()
create_documents()
if __name__ == "__main__":
main()
We define indexes in our model in the ODMConfig class.
Create indexes¶
To create indexes in our database we will use the apply_indexes function.
We import apply_indexes from mongodb_odm.
import os
from typing import Optional
from mongodb_odm import ASCENDING, Document, IndexModel, apply_indexes, connect
class Player(Document):
name: str
country_code: str
rating: Optional[int] = None
class ODMConfig(Document.ODMConfig):
indexes = [
IndexModel([("contact_email", ASCENDING)]),
]
def create_documents():
Player(name="Pelé", country_code="BRA").create()
Player(name="Diego Maradona", country_code="ARG", rating=97).create()
def main():
connect(os.environ.get("MONGO_URL", "mongodb://localhost:27017/testdb"))
apply_indexes()
create_documents()
if __name__ == "__main__":
main()
We define indexes in our model. But indexes won't be affected or created until we explicitly call apply_indexes.
Call the apply_indexes function to create indexes in the database.
Update Indexes¶
We add some changes in the index definition.
import os
from typing import Optional
from mongodb_odm import DESCENDING, Document, IndexModel, apply_indexes, connect
class Player(Document):
name: str
country_code: str
rating: Optional[int] = None
class ODMConfig(Document.ODMConfig):
indexes = [
IndexModel([("country_code", DESCENDING)]),
]
def create_documents():
Player(name="Pelé", country_code="BRA").create()
Player(name="Diego Maradona", country_code="ARG", rating=97).create()
def main():
connect(os.environ.get("MONGO_URL", "mongodb://localhost:27017/testdb"))
apply_indexes()
create_documents()
if __name__ == "__main__":
main()
We made a simple change to check the update mechanism. We changed ASCENDING to DESCENDING index for country_code.
Let's look at the database and select the indexes tab for the player collection.
We should see two indexes. _id is the default index created by MongoDB and country_code_1 is the newly created index.

The country_code_1 is created as an index field in the player collection and the field should be unique as we defined in ODMConfig and also reflected in the index property as UNIQUE.
Configure CLI¶
Make sure the apply_indexes function is called after configuring the connection. We can configure the CLI to call apply_indexes.
It's recommended to use the Typer package to call apply_indexes from CLI.