MongoDB Index Types and Query Patterns

Single-field, compound, multikey, text, wildcard, partial, TTL, and unique index patterns for MongoDB.

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

MongoDB index types

Match index type to the document shape and query shape.

Single-field index

Fast equality lookups on one field.

javascriptANYmongodbsingle-field
javascript
db.users.createIndex({ email: 1 })
Notes

Ideal for identity or lookup fields.

Compound index

Support multi-field filters and sorts.

javascriptANYmongodbcompound
javascript
db.orders.createIndex({ tenantId: 1, status: 1, createdAt: -1 })
Notes

Column order still matters in MongoDB compound indexes.

Multikey index on an array field

Index documents that contain array values.

javascriptANYmongodbmultikeyarrays
javascript
db.products.createIndex({ tags: 1 })
Notes

MongoDB automatically creates a multikey index when the indexed field contains arrays.

Partial index

Index only documents matching a filter.

javascriptANYmongodbpartial-index
javascript
db.orders.createIndex(
  { createdAt: -1 },
  { partialFilterExpression: { shippedAt: { $exists: false } } }
)
Notes

A strong way to shrink index size for hot subsets such as unshipped orders.

TTL index

Expire documents automatically after a period.

javascriptANYmongodbttl
javascript
db.sessions.createIndex({ expiresAt: 1 }, { expireAfterSeconds: 0 })
Notes

Useful for sessions, caches, temporary tokens, and ephemeral telemetry.

Wildcard index

Support flexible queries on unknown or varying fields.

javascriptANYmongodbwildcard
javascript
db.events.createIndex({ "$**": 1 })
Notes

Wildcard indexes can help when documents contain highly variable keys, though they should be used carefully.

Verify with explain

Measure whether an index reduced keys and docs examined.

Explain a query with execution stats

Inspect planner and runtime behavior.

javascriptANYmongodbexplainexecutionStats
javascript
db.orders.find({ tenantId: 7, status: "paid" }).sort({ createdAt: -1 }).explain("executionStats")
Notes

Check keys examined, docs examined, winning plan, and whether the compound index is actually used.

Recommended next

No recommendations yet.