MongoDB Indexes, Explain Plans, and Performance

Create indexes, inspect query plans, and troubleshoot common MongoDB query performance issues.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Create indexes

The most common index patterns for apps and APIs.

Create a single-field index

Index one frequently filtered field.

javascriptANYmongodbindexcreateIndex
javascript
db.users.createIndex({ email: 1 })
Notes

A single-field index is often the first optimization for lookup-heavy fields.

Create a compound index

Index fields used together in filters or sorts.

javascriptANYmongodbindexcompound
javascript
db.orders.createIndex({ status: 1, created_at: -1 })
Notes

Compound indexes are useful when queries filter and sort on a predictable pattern.

Enforce uniqueness with an index

Prevent duplicate values at write time.

javascriptANYmongodbindexunique
javascript
db.users.createIndex({ email: 1 }, { unique: true })
Notes

Great for usernames, emails, external ids, and other unique keys.

Create a text index

Enable text search over string content.

javascriptANYmongodbindextext-search
javascript
db.articles.createIndex({ title: "text", body: "text" })
Notes

Text indexes support MongoDB text search syntax over indexed fields.

List indexes on a collection

Inspect existing indexes.

javascriptANYmongodbindexesinspection
javascript
db.users.getIndexes()
Notes

Useful before adding more indexes or debugging an execution plan.

Explain and tune

See what the query planner is doing and validate index usage.

Explain a query

Show the execution plan for a `find()` query.

javascriptANYmongodbexplainperformance
javascript
db.orders.find({ status: "paid" }).sort({ created_at: -1 }).explain("executionStats")
Notes

`executionStats` gives practical detail such as scanned docs, scanned keys, and returned docs.

Force a specific index with hint

Test how a query behaves with a chosen index.

javascriptANYmongodbhintindexes
javascript
db.orders.find({ status: "paid" }).hint({ status: 1, created_at: -1 })
Notes

Helpful for diagnostics, but avoid overusing hints in app code unless there is a strong reason.

Hide an index for testing

Temporarily hide an index from the planner.

javascriptANYmongodbhideIndexindexes
javascript
db.orders.hideIndex({ status: 1, created_at: -1 })
Notes

Hidden indexes can help you test whether an index is still needed before dropping it.

Inspect collection stats

View storage and index metrics for a collection.

javascriptANYmongodbcollStatsstats
javascript
db.runCommand({ collStats: "orders" })
Notes

Useful for checking total size, avg document size, and index footprint.

Drop an index

Remove an unneeded index.

javascriptANYmongodbdropIndexindexes
javascript
db.orders.dropIndex({ status: 1, created_at: -1 })
Notes

Every index speeds some reads but adds write and storage cost, so removing unused ones can help.

Recommended next

No recommendations yet.