Skip to content

MongoDB Indexes

Indexes are an important tool for improving query performance in MongoDB. They are similar to the table of contents in a book, allowing MongoDB to quickly locate and access data without scanning the entire collection.

Basic Concepts

How Indexes Work

Indexes in MongoDB are stored as B-tree structures, which allow query operations to quickly locate the required data. When we create an index on a collection, MongoDB creates a separate data structure that contains the values of the index field and pointers to the corresponding documents in the collection.

Types of Indexes

  1. Single Field Index: Index created on a single field
  2. Compound Index: Index created on multiple fields
  3. Multikey Index: Index created on array fields
  4. Geospatial Index: Index used for geospatial queries
  5. Text Index: Index used for text search
  6. Hashed Index: Index used for hash queries

Creating Indexes

Creating Single Field Indexes

javascript
// Create an ascending index for the name field
db.users.createIndex({ name: 1 })

// Create a descending index for the age field
db.users.createIndex({ age: -1 })

Creating Compound Indexes

javascript
// Create a compound index for the name and age fields
db.users.createIndex({ name: 1, age: -1 })

Creating Unique Indexes

javascript
// Create a unique index for the email field
db.users.createIndex({ email: 1 }, { unique: true })

Creating Indexes in the Background

By default, index creation operations block other operations. We can use the background option to create indexes in the background.

javascript
// Create an index for the name field in the background
db.users.createIndex({ name: 1 }, { background: true })

Viewing Indexes

Viewing All Indexes in a Collection

javascript
db.users.getIndexes()

Viewing the Size of Indexes

javascript
db.users.stats()

Dropping Indexes

Dropping a Single Index

javascript
// Drop the index on the name field
db.users.dropIndex({ name: 1 })

// Or drop the index using the index name
db.users.dropIndex("name_1")

Dropping All Indexes

javascript
db.users.dropIndexes()

Using Indexes

Using Indexes in Queries

MongoDB automatically selects the appropriate index to optimize queries. We can use the explain() method to verify whether a query uses an index.

javascript
// Query for documents where age is greater than 30 and verify if an index is used
db.users.find({ age: { $gt: 30 } }).explain()

Forcing the Use of a Specific Index

We can use the hint() method to force a query to use a specific index.

javascript
// Force the query to use the age_1 index
db.users.find({ age: { $gt: 30 } }).hint("age_1")

Index Optimization

Index Selectivity

Index selectivity is the ratio of the number of distinct values in the index to the number of documents in the collection. Indexes with higher selectivity have better query performance.

Covered Queries

A covered query is a query where both the query fields and the sort fields are included in the index, thus avoiding scanning the collection.

javascript
// Query for name and age fields and sort by name field
// If there is a compound index { name: 1, age: 1 }, a covered query will be used
db.users.find({ name: /^J/ }, { name: 1, age: 1 }).sort({ name: 1 })

Avoiding Over-Indexing

While indexes can improve query performance, too many indexes can slow down write operations. We should create an appropriate number of indexes based on actual needs.

Common Questions

Index Creation Time

The time to create an index depends on the size of the collection and the type of the index field. Creating indexes on large collections may take a long time.

Index Storage

Indexes take up additional storage space. We should regularly check the size of indexes and optimize them as needed.

Index Maintenance

Indexes are automatically updated when data in the collection changes. This can slow down write operations, especially on large collections.

Summary

Indexes are an important tool for improving query performance in MongoDB. We can create different types of indexes as needed to optimize query operations. At the same time, we also need to pay attention to index maintenance and optimization to ensure continuous improvement of query performance.

Content is for learning and research only.