A MongoDB index is a data structure that helps MongoDB find documents quickly. Indexes are created on one or more fields in a collection. When you create an index, MongoDB stores the values of the indexed fields in a separate data structure. This data structure is called the B-tree.
When you run a query on a collection with an index, MongoDB can use the index to find the documents that match the query without having to scan the entire collection. This can make queries much faster.
There are two types of indexes in MongoDB:
Single-field indexes: Single-field indexes are created on a single field in a collection.
Multi-field indexes: Multi-field indexes are created on multiple fields in a collection.
To create an index in MongoDB, you use the following command:
<direction> is the direction of the index. The possible values are 1 for ascending and -1 for descending.
For example, the following command creates a single-field index on the name field in the users collection:
Code snippet
db.users.createIndex( { name: 1 } )
This index will be used to find documents in the users collection where the name field is in ascending order.
You can also create multi-field indexes. For example, the following command creates a multi-field index on the name and age fields in the users collection:
Code snippet
db.users.createIndex( { name: 1, age: 1 } )
This index will be used to find documents in the users collection where the name field is in ascending order and the age field is in ascending order.
Indexes can improve the performance of queries, but they also take up space. It is important to create indexes only on fields that are frequently used in queries
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
A MongoDB index is a data structure that helps MongoDB find documents quickly. Indexes are created on one or more fields in a collection. When you create an index, MongoDB stores the values of the indexed fields in a separate data structure. This data structure is called the B-tree.
When you run a query on a collection with an index, MongoDB can use the index to find the documents that match the query without having to scan the entire collection. This can make queries much faster.
There are two types of indexes in MongoDB:
To create an index in MongoDB, you use the following command:
Code snippet
where:
For example, the following command creates a single-field index on the name field in the users collection:
Code snippet
This index will be used to find documents in the users collection where the name field is in ascending order.
You can also create multi-field indexes. For example, the following command creates a multi-field index on the name and age fields in the users collection:
Code snippet
This index will be used to find documents in the users collection where the name field is in ascending order and the age field is in ascending order.
Indexes can improve the performance of queries, but they also take up space. It is important to create indexes only on fields that are frequently used in queries