Video Transcript (English)
MongoDB provides a flexible and dynamic indexing system that allows developers to create, modify, hide, and drop indexes on collections at any time without any database downtime.
So as your application evolves, you can have confidence that your indexing strategy can evolve too without the pain and complexity!
In this video, we’ll show you how to create indexes with the MongoDB Shell, or mongosh. Then, we’ll discuss managing indexes over time.
Let’s take a moment to look at where we are in managing the indexing lifecycle for our bank messaging app.
We identified queries for key workloads that can benefit from indexes and we planned our indexing strategy to support those queries using as few indexes as possible.
Our next steps are to create those indexes on the messages collection and manage those indexes.
MongoDB offers several options for creating and managing indexes.
The Atlas Data explorer, the Atlas CLI, MongoDB Compass, MongoDB Shell, and the MongoDB drivers all use the createIndex command when building an index.
In this demo, we’ll focus on running the createIndexes Database Command in the MongoDB shell.
Here, we’re using the createIndexes database command to create multiple indexes with different properties.
We create the first index that will support queries one and two, then we create a partial index to support the third query.
Note that when we use the database command to create our indexes, we need to provide a name for each index.
We recommend using createIndexes whenever possible to efficiently create multiple indexes in a single operation.
This optimizes resource use and reduces the impact on database performance.
When you run the command, you will receive a success message that looks something like this.
We can see that the operation succeeded here and that two indexes were created. This collection started with one default index on _id, and we just created two more, so now there are three indexes in total.
Great! Now that we’ve created our indexes, let’s talk about index management.
Index management in MongoDB involves several key practices, including planning, monitoring, and maintaining indexes as your data and application evolve.
We’ve covered planning in this skill, let’s talk about monitoring and maintaining indexes.
Maintenance is a continuous process, where you periodically evaluate how often your indexes are being used, hide indexes that you want to consider dropping to evaluate the impact, drop unused or redundant indexes, and update others to align with changing query requirements.
Here are some of the mongosh methods that can help with monitoring and maintenance. getIndexes returns a list of all indexes present on a given collection.
This is a helpful method to use to confirm that your indexes were created.
The hideIndexes() method allows you to hide an existing index from the query planner so that it will not be used to support your queries.
MongoDB recommends that you use this method to evaluate the impact of dropping an index before actually doing so.
If you decide that an index needs to be removed after testing it out with hideIndex(), dropIndex() or dropIndexes(), can be used to remove indexes from a collection.
Finally, the explain() method provides the query execution plan, showing how MongoDB processes the query and utilizes available indexes.
We’ll learn more about the explain() method later.
For more information on these methods and tools, check out MongoDB’s documentation.
To monitor and maintain your indexes, you can also use: MongoDB Atlas, MongoDB Compass, or third party tools
These options can help you by providing insight into index performance and usage patterns, index efficiency, and detecting unused or redundant indexes.
For more automated guidance, MongoDB Atlas offers the Performance Advisor, a powerful tool that analyzes your query patterns and recommends indexes that could enhance performance.
Nice work! Let’s recap what we covered in this lesson:
First we learned how to create indexes in the MongoDB shell using the createIndexes database command.
Then we discussed Index management in MongoDB, which involves planning, monitoring, and maintaining indexes as your data and application evolve.
Finally we discussed tools and methods that can be used to monitor queries and indexes, like: Performance Advisor with MongoDB Atlas, MongoDB Compass, MongoDB shell methods, or third party tools.
