Indexes, Compound Indexes, Text Search, Geospatial Indexes, and Performance Basics
Learn how MongoDB indexing works and why performance depends heavily on matching indexes to real query patterns.
Inside this chapter
- Why Indexes Matter
- Simple and Compound Indexes
- Specialized Index Types
- Index Tradeoffs
Series navigation
Study the chapters in order for the clearest path from MongoDB basics to advanced document modeling and production operations. Use the navigation at the bottom of each page to move through the full series.
Why Indexes Matter
A query that feels fast with a few hundred documents can become very slow with millions if MongoDB must scan too much data. Indexes help the database locate relevant documents faster for filtering and sorting operations.
Simple and Compound Indexes
db.orders.createIndex({ customerId: 1 })
db.orders.createIndex({ customerId: 1, createdAt: -1 })
A simple index supports filtering on one field. A compound index supports combined query and sort patterns more efficiently when ordered well.
Specialized Index Types
MongoDB also supports text indexes for text search scenarios and geospatial indexes for location-aware queries. These extend MongoDB beyond simple key-based lookups and make it more useful for specialized application workloads.
Index Tradeoffs
Indexes improve reads but consume storage and can slow writes. Strong index design comes from measuring actual workload patterns rather than creating many indexes blindly.