Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Mastering MongoDB 7.0 - Fourth Edition

You're reading from  Mastering MongoDB 7.0 - Fourth Edition

Product type Book
Published in Jan 2024
Publisher Packt
ISBN-13 9781835460474
Pages 434 pages
Edition 4th Edition
Languages
Concepts
Authors (7):
Marko Aleksendrić Marko Aleksendrić
Profile icon Marko Aleksendrić
Arek Borucki Arek Borucki
Profile icon Arek Borucki
Leandro Domingues Leandro Domingues
Profile icon Leandro Domingues
Malak Abu Hammad Malak Abu Hammad
Profile icon Malak Abu Hammad
Elie Hannouch Elie Hannouch
Profile icon Elie Hannouch
Rajesh Nair Rajesh Nair
Profile icon Rajesh Nair
Rachelle Palmer Rachelle Palmer
Profile icon Rachelle Palmer
View More author details

Table of Contents (20) Chapters

Preface Chapter 1: Introduction to MongoDB Chapter 2: The MongoDB Architecture Chapter 3: Developer Tools Chapter 4: Connecting to MongoDB Chapter 5: CRUD Operations and Basic Queries Chapter 6: Schema Design and Data Modeling Chapter 7: Advanced Querying in MongoDB Chapter 8: Aggregation Chapter 9: Multi-Document ACID Transactions Chapter 10: Index Optimization Chapter 11: MongoDB Atlas: Powering the Future of Developer Data Platforms Chapter 12: Monitoring and Backup in MongoDB Chapter 13: Introduction to Atlas Search Chapter 14: Integrating Applications with MongoDB Chapter 15: Security Chapter 16: Auditing Chapter 17: Encryption Index Other Books You May Enjoy

Index Optimization

Every database system, whether relational or non-relational, relies on indexing to speed up data querying. This is why index optimization is crucial for using the database to its full speed and potential. MongoDB features a powerful granular indexing system that can improve the overall performance of the database.

This chapter discusses how MongoDB supports a variety of indexes, ranging from single-field to compound indexes, including geospatial, hashed, and partial indexes. Additionally, it covers recently introduced indexing types, such as compound wildcard indexes.

This chapter will cover the following topics:

  • Introduction to indexing and its collections
  • Defining indexes in MongoDB
  • Types of indexes and strategies for optimal use
  • Best practices and considerations

Introduction to indexes

An index is a special data structure that enables faster access to data in a collection, much like an old-school paper encyclopedia index. It is an ordered list of references to the actual contents—the documents—which allows MongoDB to query much faster, often by orders of magnitude. Indexes store values of a specific field or a set of fields, ordered by value. As you will see later in The equality, sort, range (ESR) rule section, MongoDB can return sorted results using the ordering of the index itself.

MongoDB indexes use a data structure known as B-tree, a self-balancing tree data structure that maintains sorted data, and allows sequential access, searches, insertions, and deletions in logarithmic time. The index can be thought of as a list of key-value pairs, where each key is a value of index, and the value of the key-value pair is the document itself. Like an index of a book, the keys are stored in order, and associated with one or more...

Index types

In the following section, you will explore different index types, understanding how and when to utilize them for optimal outcomes.

Single-field indexes and indexing embedded documents

The simplest and the most common type of index is the single-field index. By default, MongoDB automatically generates single-field indexes for every collection—a unique index on the _id field. This index prevents the insertion of a second document with the same value for the _id field into the collection.

In the previous section, you saw an example of the simplest possible single-field index. You can further enhance your books collection by adding a couple more indexes—one for the titles and one for the price, which is an embedded field, to make it unique.

Execute the following command:

db.books.createIndex({ "title": 1 }, { "unique": true })

Note

Two indexes cannot have the same shape even in cases when the options are different. For...

Best practices for index optimization

Now that you have covered a lot of ground and explored various MongoDB indexes, let's look at the best practices and guidelines for optimizing indexes.

Creating indexes for your MongoDB database is an important task that can—and will—determine the speed and performance of the queries and the overall database performance. As you've seen throughout this chapter, creating indexes is easy and intuitive, and there are many types of indexes that cover almost every possible use case. However, having too many or inadequate indexes can hamper the database performance, and special care should be taken when deciding on what to index and how to index.

One of the most important features of an index is its selectivity—the measure of the number of documents associated with a single index value. An index is said to be selective if it has many unique values and few duplicates. Cardinality, on the other hand, refers to several...

Summary

In this chapter, you discovered MongoDB indexes, their types, and their uses. You explored how carefully crafted indexes can speed up your queries, and gained insights into best practices for database optimization. You learned about single-field indexes, compound indexes, wildcard indexes, and multikey indexes, illustrating how each can contribute to optimizing queries and aggregations.

Additionally, the chapter delved into specialized indexes, including geospatial, hidden, and the newly introduced compound wildcard index, addressing the challenge of indexing fields not known in advance. Finally, it delved into tips and strategies for constructing optimized indexes to maximize the efficiency of your queries.

In the next chapter, you will learn about MongoDB Atlas—the most advanced cloud database service on the market.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Mastering MongoDB 7.0 - Fourth Edition
Published in: Jan 2024 Publisher: Packt ISBN-13: 9781835460474
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}