
#54 Creating a Compound Index | Understanding Index in MongoDB | MongoDB Complete Course 2025
procademy
Overview
This video explains the concept and application of compound indexes in MongoDB. It differentiates compound indexes from single-field indexes and highlights why certain fields, like booleans or low-cardinality fields (e.g., gender), are not ideal for indexing. The core of the lecture focuses on how to create a compound index on multiple fields, emphasizing that the order of fields in the index definition is crucial for query performance. It demonstrates how MongoDB utilizes compound indexes for queries that include the leading fields of the index, and how it falls back to collection scans if the leading fields are omitted.
Save this permanently with flashcards, quizzes, and AI chat
Chapters
- Indexes on fields with very few distinct values (low cardinality), like booleans (true/false) or gender (male/female), are generally inefficient.
- When a query on an indexed field returns a majority of the documents, the index becomes less effective or even detrimental to performance.
- MongoDB might still use an index on a low-cardinality field, but it involves extra steps (index scan then document fetch), negating performance benefits.
- A compound index is a single index that covers two or more fields within a collection.
- Compound indexes allow for more efficient querying when multiple fields are used in the query criteria.
- You can define compound indexes on up to 31 fields.
- Compound indexes are created using the `createIndex()` method, specifying fields and their order (e.g., `{ age: 1, gender: 1 }`).
- The order of fields in the `createIndex()` definition is critical; it determines how the index is structured and utilized.
- An index defined as `{ age: 1, gender: 1 }` sorts data first by 'age' and then by 'gender' within each age group.
- MongoDB can use a compound index if the query filters include the *leading* fields of the index, in order.
- Queries that include the first field of the index (e.g., 'age' in `{ age: 1, gender: 1 }`) can utilize the index, even if subsequent fields are omitted.
- Queries that omit the first field of the index but include later fields (e.g., only 'gender' in `{ age: 1, gender: 1 }`) will *not* use the index and will result in a collection scan.
- The order of fields within the query's filter object does not matter; only the presence and order relative to the index definition matter.
Key takeaways
- Avoid creating indexes on fields with very low cardinality (few unique values) as they offer little to no performance benefit.
- Compound indexes are single indexes that span multiple fields, improving performance for queries involving those fields.
- The order of fields specified when creating a compound index is crucial for its effectiveness.
- MongoDB can use a compound index if the query filters match the index's fields from left to right (the leading fields).
- If a query omits the leading field(s) of a compound index, MongoDB will not use that index and will perform a collection scan.
- The order of fields in the query's filter condition does not affect index usage, but the fields included and their relationship to the index's defined order do.
Key terms
Test your understanding
- Why are indexes on boolean or gender fields generally not recommended in MongoDB?
- What is a compound index and how does it differ from a single-field index?
- How does the order of fields in the `createIndex()` command affect the performance of a compound index?
- Under what conditions will MongoDB utilize a compound index for a query?
- What happens if a query attempts to use a compound index but omits the first field defined in that index?