Imagine you’re searching for a book in a library where the books are piled into a mountain without a catalog—you have to flip through each one. That’s what a MongoDB database without an index is like. With a catalog (index), you can quickly locate the target book, which is exactly what MongoDB indexes do. When your database has a large amount of data (e.g., hundreds of thousands or millions of documents), a “full table scan” without an index will make queries extremely slow, while a reasonable index can make the query speed “fly,” even increasing it by more than 10 times.
I. Why MongoDB Indexes Are Needed?¶
In MongoDB, documents in each collection (equivalent to a “table”) are stored in an unordered manner by default. When you execute a query (e.g., db.users.find({age: 25})), if there’s no index, MongoDB will start from the first document and check each document’s age field one by one to see if it equals 25 until all matching documents are found. This “full table scan” has a time complexity of O(n) (where n is the total number of documents), which is like “fishing for a needle in a haystack” when the data volume is large.
An index, however, is like creating a “catalog” for a specific field (e.g., age), storing the field’s values and corresponding document positions. During a query, MongoDB directly locates the target document through the index, reducing the time complexity to O(log n) (logarithmic level)—this is drastically more efficient than searching through a catalog.
II. What Are MongoDB Indexes?¶
MongoDB indexes are a special data structure (typically a B-tree/B+ tree at the bottom) that stores values of certain fields in the collection and points to the corresponding document locations. For example, if you create an index on the age field of the users collection, MongoDB will automatically maintain an ordered list of age values, with each age value pointing to a specific document.
In simple terms: An index is a “field value → document position” mapping table, allowing queries to skip irrelevant data and directly find the target.
III. Basic Types of MongoDB Indexes¶
1. Single-Field Index (Most Common)¶
Create an index for a single field. For example, create an index on the age field:
// Create a single-field index: 1 for ascending order, -1 for descending order
db.users.createIndex({ age: 1 })
During a query, if the condition includes the age field, MongoDB will automatically use this index.
2. Compound Index¶
When a query involves multiple fields, a compound index is more efficient. For example, if you frequently query users with “age > 20 and gender = male,” you can create a compound index:
// Compound index: first sort by age ascending, then by gender ascending
db.users.createIndex({ age: 1, gender: 1 })
Note: Compound indexes follow the “leftmost prefix rule”—the query must include the leftmost field of the index (e.g., age) to trigger the index. If you only query gender, the compound index won’t be used.
3. Other Advanced Indexes (Optional Knowledge)¶
- Multikey Index: For array fields (e.g.,
tags: ["a", "b"]), MongoDB creates an index for each element in the array. - Geospatial Index: Used for coordinate-based queries (e.g., “nearby people”).
- Text Index: Supports full-text search (e.g., searching “MongoDB tutorial”).
IV. How to Create and Verify Indexes?¶
1. View Existing Indexes in a Collection¶
db.users.getIndexes() // Outputs all index information
2. Create an Index and Verify Its Effect¶
Example: Suppose we have a users collection with 1 million user records, and the age field is frequently queried.
- Create the Index:
db.users.createIndex({ age: 1 }) // Create an ascending index on the age field
- Verify the Index Works:
Use theexplain()method to check the query plan and confirm if the index is used. For example:
// Check if the query uses an index
db.users.find({ age: 25 }).explain("executionStats")
In the output, look at the executionStats section:
- executionTimeMillis: Execution time (smaller is better).
- totalDocsExamined: Number of documents scanned (should be much smaller than the total number of documents with an index).
- executionSuccess: Should be true.
V. When to Create an Index? When Not To?¶
Scenarios for Creating Indexes:¶
- Frequently Queried Fields: E.g.,
username,email(frequently used as query conditions). - Sorting Fields: E.g.,
createTime(frequently queried with time descending). - Field Combinations for Compound Queries: E.g.,
age+gender(filtering multiple conditions simultaneously).
Scenarios for Avoiding Indexes:¶
- Small Data Volume: E.g., only 10 records; a full table scan is faster.
- Extremely Frequent Writes: E.g.,
status(maintaining indexes slows down insert/update/delete operations). - Low Cardinality Fields: E.g.,
gender(only two values: “male/female”; full table scan is faster). - Highly Repeated Fields: E.g.,
isActive: true(almost all documents aretrue; the index is meaningless).
VI. Index Pitfalls and Precautions¶
- Avoid Over-Indexing: Each index occupies storage space and slows down writes (inserts/updates/deletes require index maintenance).
- Avoid Duplicate Indexes: Existing indexes won’t be reused; repeated creation wastes resources.
- Verify Index Usage with
explain(): Even after creating an index, check if it’s actually used (e.g., if the query condition doesn’t match the index field, the index becomes invalid). - Beware of “Field Type Mismatch”: If the index is of string type but the query uses a number type, the index will be invalid.
VII. Summary¶
MongoDB indexes are a core means to improve query performance. Using them reasonably can make query speed “fly.” From today onward, try creating single-field or compound indexes for frequently queried fields in your project and verify the effect with explain(). Remember: more indexes aren’t better—indexes should be “created on demand and optimized precisely.”
Now, open your MongoDB client and create your first index for a collection!