MongoDB Index Explained: When and How to Use Them
Indexes are one of the most important tools for MongoDB performance. Without them, every query scans your entire collection. With the right indexes, the same query can be orders of magnitude faster. Here's what you need to know.
What Is an Index?
An index in MongoDB is a data structure that stores a small portion of your collection's data in a form that's efficient to search. Think of it like an index in a book — instead of reading every page to find a topic, you look up the page number in the index and go directly there.
Without an index, MongoDB performs a collection scan (COLLSCAN) — it reads every document to find matches. With an index, it performs an index scan (IXSCAN) — it uses the index to jump directly to matching documents.
Every collection has a default index on the _id field. For everything else, you need to create indexes yourself.
Types of MongoDB Indexes
Single Field Index
The simplest type. It indexes one field in ascending or descending order. Use this when you frequently query or sort by a single field.
// Index on the email field (ascending)
db.users.createIndex({ "email": 1 })Compound Index
Indexes multiple fields together. The order of fields matters — MongoDB uses the index from left to right. Compound indexes support queries that filter on any prefix of the indexed fields.
// Compound index: status + createdAt
db.orders.createIndex({ "status": 1, "createdAt": -1 })This index supports queries filtering by status alone, or by status and createdAt together. It does not efficiently support queries filtering only by createdAt.
Text Index
Enables full-text search on string fields. Useful for searching product descriptions, article content, or user comments.
// Text index on product name and description
db.products.createIndex({
"name": "text",
"description": "text"
})TTL Index (Time-To-Live)
Automatically deletes documents after a specified number of seconds. Perfect for session data, temporary tokens, or log entries that should expire.
// Delete documents 24 hours after createdAt
db.sessions.createIndex(
{ "createdAt": 1 },
{ "expireAfterSeconds": 86400 }
)When to Create an Index
Not every field needs an index. Here are the situations where indexes provide the most benefit:
- Frequent queries — if you filter or sort by a field often, index it.
- Large collections — the bigger the collection, the more a collection scan hurts performance.
- Unique constraints — use a unique index to prevent duplicate values (e.g., email addresses).
- Sort operations — indexes can satisfy sort orders without an in-memory sort, which has a 100MB limit.
When Not to Index
Indexes aren't free. They come with trade-offs:
- Write overhead — every insert, update, and delete must also update the index. Too many indexes slow down writes.
- Storage cost — indexes consume disk space and RAM. An index on a large collection can be significant.
- Low selectivity — indexing a boolean field like
isActiverarely helps because the index only has two distinct values. - Small collections — if a collection has a few hundred documents, a full scan is fast enough. The overhead of maintaining an index isn't worth it.
The goal is to index fields that are queried often on large collections, while avoiding unnecessary indexes that slow down writes without improving reads.
Managing Indexes with Mongon
Managing indexes in the MongoDB shell means running commands, remembering syntax, and parsing JSON output. A GUI makes this much more approachable.
Mongon lets you view and manage indexes visually on macOS:
- View all indexes — see every index on a collection at a glance, including the indexed fields, type, and options like unique or TTL.
- Create indexes — add new indexes through the UI without writing
createIndexcommands. - Drop indexes — remove unused indexes that are slowing down writes, with a clear confirmation step.
- Native performance — built with SwiftUI, Mongon is fast and lightweight. No Electron or Java overhead. See our Apple Silicon performance comparison.
Being able to see your indexes alongside your query results helps you understand performance. If a query is slow, you can check which indexes exist, spot what's missing, and add one — all without leaving the app.
Best Practices for MongoDB Indexes
- Follow the ESR rule — for compound indexes, order fields by Equality, Sort, then Range. This gives the most efficient index usage.
- Use explain() — run
.explain("executionStats")to see whether your query uses an index and how many documents it examines. - Avoid over-indexing — each index has a cost. Audit your indexes periodically and drop ones that aren't used.
- Consider covered queries — if an index includes all the fields your query needs, MongoDB can return results from the index alone without reading the full documents.
Get a Clear View of Your Indexes
Indexes are essential for MongoDB performance, but managing them shouldn't require memorizing shell commands. Mongon gives you a visual, native macOS interface to view, create, and manage indexes alongside your data. Free plan available — no subscription required.