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
Single-field index
db.users.createIndex({ email: 1 })

# Fast equality lookups on one field.

Compound index
db.orders.createIndex({ tenantId: 1, status: 1, createdAt: -1 })

# Support multi-field filters and sorts.

Multikey index on an array field
db.products.createIndex({ tags: 1 })

# Index documents that contain array values.

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

# Index only documents matching a filter.

TTL index
db.sessions.createIndex({ expiresAt: 1 }, { expireAfterSeconds: 0 })

# Expire documents automatically after a period.

Wildcard index
db.events.createIndex({ "$**": 1 })

# Support flexible queries on unknown or varying fields.

## Verify with explain
Explain a query with execution stats
db.orders.find({ tenantId: 7, status: "paid" }).sort({ createdAt: -1 }).explain("executionStats")

# Inspect planner and runtime behavior.

Recommended next

No recommendations yet.