Docs Home → Develop Applications → Python Drivers → PyMongo
Work with Indexes
On this page
Overview
In this guide, you can learn how to use indexes with PyMongo. Indexes can improve the efficiency of queries and add additional functionality to querying and storing documents.
Without indexes, MongoDB must scan every document in a collection to find the documents that match each query. These collection scans are slow and can negatively affect the performance of your application. However, if an appropriate index exists for a query, MongoDB can use the index to limit the documents it must inspect.
Operational Considerations
To improve query performance, build indexes on fields that appear often in your application's queries and operations that return sorted results. Each index that you add consumes disk space and memory when active, so we recommend that you track index memory and disk usage for capacity planning. In addition, when a write operation updates an indexed field, MongoDB updates the related index.
Because MongoDB supports dynamic schemas, applications can query against fields whose names are not known in advance or are arbitrary. MongoDB 4.2 introduced wildcard indexes to help support these queries. Wildcard indexes are not designed to replace workload-based index planning.
For more information about designing your data model and choosing indexes appropriate for your application, see the Data Modeling and Indexes guide in the MongoDB Server manual.
Sample Data
The examples in this guide use the sample_mflix.movies
collection
from the Atlas sample datasets. To learn how to create a
free MongoDB Atlas cluster and load the sample datasets, see the
Get Started with PyMongo.
Create an Index
MongoDB supports several different index types to support querying your data. The following pages describe the most common index types and provide sample code for creating each index type.
Remove an Index
You can remove any unused index except the default unique index on the
_id
field.
The following sections show how to remove a single index or to remove all indexes in a collection.
Remove a Single Index
Pass an instance of an index or the index name to the drop_index()
method to
remove an index from a collection.
The following example removes an index with the name "_title_"
from the movies
collection:
movies.drop_index("_title_")
Note
You cannot remove a single field from a compound text index. You must drop the entire index and create a new one to update the indexed fields.
Remove All Indexes
Starting with MongoDB 4.2, you can drop all indexes by calling the
drop_indexes()
method on your collection:
collection.drop_indexes()
For earlier versions of MongoDB, pass "*"
as a parameter to your call to
drop_index()
on your collection:
collection.drop_index("*")
Additional Information
To learn more about indexes in MongoDB, see the Indexes guide in the MongoDB Server manual.
API Documentation
To learn more about any of the methods or types discussed in this guide, see the following API documentation: