Skip to content
_CORE
AI & Agentic Systems Core Information Systems Cloud & Platform Engineering Data Platform & Integration Security & Compliance QA, Testing & Observability IoT, Automation & Robotics Mobile & Digital Banking & Finance Insurance Public Administration Defense & Security Healthcare Energy & Utilities Telco & Media Manufacturing Logistics & E-commerce Retail & Loyalty
References Technologies Blog Know-how Tools
About Collaboration Careers
CS EN DE
Let's talk

MongoDB Indexing

05. 04. 2024 Updated: 27. 03. 2026 1 min read intermediate

Indexes dramatically speed up queries in MongoDB.

Types

  • Single field
  • Compound
  • Multikey (arrays)
  • Text
  • Geospatial
  • TTL (auto-delete)

Examples

db.orders.createIndex({userId:1,status:1,createdAt:-1})
db.sessions.createIndex({expiresAt:1},{expireAfterSeconds:0})

Explain

db.orders.find({userId:123}).explain('executionStats')
// COLLSCAN=bad, IXSCAN=good
  • ESR: Equality, Sort, Range
  • Partial indexes
  • Covered queries
  • Check unused ones

Indexing Strategy

The ESR rule (Equality, Sort, Range) determines the optimal field order in a compound index. Place equality match fields first, then sort fields, and finally range conditions. This minimizes the number of index keys examined.

Create partial indexes for collections where you query only a subset of documents — for example, {status: "active"}. This saves space and speeds up writes. Covered queries (queries whose results are entirely contained in the index) eliminate the need to read documents from disk. Regularly check unused indexes using $indexStats, because every index slows down writes and takes up disk space.

Indexes = Performance

Without indexes = collection scan. Always explain().

mongodbindexesperformance
Share:

CORE SYSTEMS team

We build core systems and AI agents that keep operations running. 15 years of experience with enterprise IT.