codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🍃

MongoDB

21 / 65 topics
19Indexing Basics20Creating Indexes21Compound Indexes22Unique Indexes23Geospatial Indexes24Explain Command
Tutorials/MongoDB/Compound Indexes
🍃MongoDB

Compound Indexes

Updated 2026-04-20
3 min read

Compound Indexes in MongoDB

Introduction

In MongoDB, indexes are essential for optimizing query performance. While single-field indexes are useful, compound indexes provide a more powerful way to enhance query efficiency by indexing multiple fields together. This tutorial will guide you through the creation, usage, and best practices of compound indexes in MongoDB.

Understanding Compound Indexes

A compound index is an index that includes two or more fields from a collection. The order of fields in a compound index is crucial because it determines how queries are executed. MongoDB uses the most selective field first to filter documents efficiently.

Key Characteristics

  1. Order Matters: The order of fields in a compound index affects query performance and selectivity.
  2. Prefix Matching: Queries that match the prefix of a compound index can use the entire index, improving performance.
  3. Partial Usage: If a query does not include all indexed fields, MongoDB may still use the index partially.

Creating Compound Indexes

You can create compound indexes using the createIndex() method in MongoDB. Here’s how you can create different types of compound indexes:

Ascending and Descending Order

db.collection.createIndex({ field1: 1, field2: -1 })

In this example, field1 is indexed in ascending order, and field2 is indexed in descending order.

Multikey Indexes

MongoDB supports indexing arrays with multikey indexes. Each element of the array becomes a separate key in the index.

db.collection.createIndex({ field: 1 })

If field contains an array, each element will be indexed separately.

Using Compound Indexes

Compound indexes are particularly useful for queries that filter documents based on multiple fields. Here’s how MongoDB uses compound indexes:

Prefix Matching

Queries that match the prefix of a compound index can use the entire index efficiently.

db.collection.find({ field1: value1, field2: value2 })

If an index exists on { field1: 1, field2: -1 }, this query will utilize the index effectively.

Partial Usage

Even if a query does not include all indexed fields, MongoDB may still use the index partially.

db.collection.find({ field1: value1 })

For an index on { field1: 1, field2: -1 }, this query can still benefit from the index on field1.

Best Practices

Optimize Index Order

The order of fields in a compound index is critical. Place the most selective fields first to improve performance.

db.collection.createIndex({ status: 1, created_at: -1 })

In this example, assuming status has fewer unique values than created_at, placing it first can be more efficient.

Avoid Over-Indexing

While indexes speed up read operations, they slow down write operations and consume additional storage. Create only the necessary compound indexes to balance performance and resource usage.

Use Covered Queries

A covered query is one where all fields required by a query are part of an index, allowing MongoDB to return results directly from the index without accessing the collection.

db.collection.createIndex({ status: 1, created_at: -1, name: 1 })

For a query like db.collection.find({ status: "active", created_at: { $gte: new Date() } }).sort({ name: 1 }), this index can be used to cover the query.

Monitor and Analyze Index Usage

Use MongoDB’s explain() method to analyze how queries use indexes. This helps in identifying potential performance bottlenecks and optimizing indexes accordingly.

db.collection.find({ status: "active", created_at: { $gte: new Date() } }).sort({ name: 1 }).explain("executionStats")

This command provides detailed execution statistics, including the index used and the number of documents examined.

Conclusion

Compound indexes are a powerful feature in MongoDB that can significantly enhance query performance by indexing multiple fields together. By understanding how to create, use, and optimize compound indexes, you can ensure efficient data retrieval and optimal database performance. Always consider the order of fields, balance between read and write operations, and monitor index usage to maintain high-performance databases.


PreviousCreating IndexesNext Unique Indexes

Recommended Gear

Creating IndexesUnique Indexes