September 15, 2026 · Varun Sharma
MongoDB Indexes: Every Type, What It's For, and When to Actually Use It
MongoDB indexes can make queries dramatically faster—but every index also adds storage and write overhead. The goal isn't to create more indexes. It's to create the right ones.
As a MongoDB application grows, queries that once felt instant can start slowing down.
A collection may contain thousands, millions, or even billions of documents. Without appropriate indexes, MongoDB may need to examine a large number of documents to find the results you requested.
Indexes solve this problem by creating data structures that help MongoDB find documents more efficiently.
But MongoDB supports several index types, and choosing between them isn't always obvious.
Let's break them down.
What Is a MongoDB Index?
An index is a data structure that helps MongoDB locate documents without scanning the entire collection.
For example, suppose you have a users collection:
{
name: "Varun",
email: "varun@example.com",
age: 34
}If you frequently search users by email, you could create an index:
db.users.createIndex({ email: 1 })Now MongoDB can use the index when searching for a specific email.
Without an appropriate index, MongoDB may need to examine many documents.
1. Single Field Index
A single field index indexes one field.
db.users.createIndex({ email: 1 })This is one of the most common index types.
Use it when
You frequently query, sort, or filter using one field.
Examples:
Email
User ID
Status
Created date
Product ID
For example:
db.users.find({ email: "varun@example.com" })A single-field index can make this type of query much more efficient.
2. Compound Index
A compound index contains multiple fields.
db.orders.createIndex({
customerId: 1,
createdAt: -1
})This can be useful when queries commonly filter by one field and sort or filter by another.
For example:
db.orders.find({
customerId: "123"
}).sort({
createdAt: -1
})Why field order matters
The order of fields in a compound index matters.
MongoDB can efficiently use the index according to its field order and the query pattern.
So don't simply add every field you might query.
Design the index around your actual queries.
3. Multikey Index
A multikey index is used when indexing fields that contain arrays.
For example:
{
name: "Product A",
tags: ["mongodb", "database", "backend"]
}You can create:
db.products.createIndex({ tags: 1 })MongoDB can then use the index when searching for documents containing a particular array value.
db.products.find({
tags: "mongodb"
})Use it when
Your application frequently searches array fields.
Common examples include:
Tags
Categories
Skills
Product attributes
Assigned users
4. Text Index
Text indexes support text searches on string content.
For example:
db.posts.createIndex({
title: "text",
content: "text"
})You can then search using:
db.posts.find({
$text: {
$search: "MongoDB indexes"
}
})Use it when
You need basic text-search functionality across text fields.
However, if your application requires advanced search capabilities, dedicated search solutions may be more appropriate.
5. Geospatial Index
MongoDB provides geospatial indexes for location-based queries.
For example, you might store location data using GeoJSON:
{
name: "Coffee Shop",
location: {
type: "Point",
coordinates: [77.5946, 12.9716]
}
}A geospatial index can then support location queries.
db.places.createIndex({
location: "2dsphere"
})Use it for
Applications involving:
Nearby stores
Restaurants
Delivery services
Travel
Maps
Location-based services
6. Hashed Index
A hashed index stores hashed values of indexed fields.
db.users.createIndex({
userId: "hashed"
})Hashed indexes can be useful for equality-based lookups and certain sharding scenarios.
They are not designed for normal range queries.
For example, searching for:
userId = 12345is different from needing queries such as:
userId > 10000Use it when
You have a suitable equality-based access pattern or a sharding requirement that benefits from hashed distribution.
7. Wildcard Index
Wildcard indexes are useful when your documents contain flexible or unpredictable fields.
For example:
{
name: "Product",
attributes: {
color: "black",
material: "steel",
size: "large"
}
}A wildcard index can index fields within dynamic structures.
db.products.createIndex({
"attributes.$**": 1
})Use it when
Your schema contains dynamic fields and you can't reasonably create individual indexes for every possible field.
But don't use wildcard indexes simply because they're convenient.
If your query patterns are known, targeted indexes are often a better choice.
8. Unique Index
A unique index prevents duplicate values.
For example:
db.users.createIndex(
{ email: 1 },
{ unique: true }
)Now two users cannot have the same email address.
Common uses
Email addresses
Usernames
External IDs
Order numbers
Slugs
Unique indexes are especially useful because they enforce uniqueness at the database level.
9. Partial Index
A partial index only indexes documents that match a specified condition.
For example:
db.users.createIndex(
{ email: 1 },
{
partialFilterExpression: {
status: "ACTIVE"
}
}
)This means the index only contains documents matching the specified condition.
Use it when
You frequently query a subset of documents.
For example:
Active users
Published posts
Non-deleted records
Verified accounts
Partial indexes can reduce index size compared with indexing every document.
10. Sparse Index
A sparse index only contains documents where the indexed field exists.
For example:
db.users.createIndex(
{ phone: 1 },
{ sparse: true }
)Documents without a phone field aren't included in the index.
Use it when
A field is optional and you specifically want an index that excludes documents where the field doesn't exist.
However, partial indexes are often a more flexible choice when you need more precise control over which documents are indexed.
Don't Add Indexes Everywhere
One of the biggest MongoDB indexing mistakes is assuming:
"More indexes = better performance."
That's not true.
Indexes improve reads, but they also have costs.
Every additional index can mean:
More disk usage
More memory pressure
More work during inserts
More work during updates
More maintenance
If you create indexes that your application never uses, you're adding overhead without getting much benefit.
Design Indexes Around Real Queries
Before creating an index, look at how your application actually queries the database.
For example, if your application frequently runs:
db.orders.find({
customerId: "123",
status: "PAID"
})you might consider an index that reflects that access pattern.
Don't design indexes based only on your schema.
Design them around how your application reads the data.
Use explain() Before Guessing
MongoDB provides explain() to help understand query execution.
For example:
db.users.find({
email: "varun@example.com"
}).explain("executionStats")This can help you understand whether MongoDB is using an index and how much work the query is performing.
Instead of guessing:
"This index should make the query faster."
measure it.
Watch for Unused Indexes
As your application evolves, some indexes may become unnecessary.
A query pattern that was important six months ago may no longer exist.
Regularly review your indexes and remove unnecessary ones carefully.
Before dropping an index, make sure you understand which queries depend on it.
A Simple Indexing Strategy
A practical approach is:
Start with important queries
Find your most frequently executed and most expensive queries.
Create targeted indexes
Build indexes around those query patterns.
Measure performance
Use explain() and application-level monitoring.
Monitor write performance
Remember that indexes also affect writes.
Remove unnecessary indexes
Don't allow old indexes to accumulate forever.
The Biggest MongoDB Indexing Lesson
Indexes aren't something you add randomly after your application becomes slow.
They should be part of your database design.
Think about:
What data do users search for?
How is that data filtered?
How is it sorted?
Which queries run most frequently?
Then create indexes that support those patterns.
Final Thoughts
MongoDB gives developers a lot of indexing flexibility—from simple single-field indexes to compound, multikey, geospatial, partial, wildcard, and other specialized indexes.
But you don't need every index type.
The best MongoDB index isn't the one that makes your schema look optimized. It's the one that makes your real-world queries faster without creating unnecessary overhead.
Comments
Join the discussion and share your thoughts.