Conclusion / Learn

Video Transcript (English)

In this unit, you learned that MongoDB indexes support the efficient execution of queries. You also learned about the trade-offs of using indexes.

You gained a deeper understanding of the most common index types and how to create and delete them.

To create indexes in a collection, we used the db.collection.createIndex command.

We can create single-field indexes when the index only includes one field, and compound indexes if the index has more than one field.

Indexes are multikey indexes when the index includes an array field.

We also used the getIndexes command and the Indexes section in the Atlas UI to review the indexes in a collection.

Remember that indexes can include the unique constraint to ensure that the values in the fields defined by the index are unique in the collection.

Also remember that the order of the fields and the sort order of the field values matter when defining compound indexes. Use the Equality, Sort, and Range guideline, and review the sort order of the field values.

Additionally, take into account that indexes can cover queries if all the fields in the query are included in the index keys.

Finally, we can use the explain command in the collection or in the queries to determine if the index is being used.

And, we can delete any unnecessary index by using the dropIndex command. However, be aware that deleting indexes that are being used by a query will impact the query performance. Before deleting an index, hide the index first so that you can check the impact of not having the index in the collection. To hide an index, we use the hideIndex command.

Now that you’ve gained a general understanding of how to create and use indexes, you’ll be able to improve the performance of your queries in MongoDB

As a next step, review MongoDB’s documentation on Indexes.

Congratulations on completing this unit!