_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
Let's talk

MongoDB Indexing

05. 04. 2024 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

Indexes = Performance

Without indexes = collection scan. Always explain().

mongodbindexesperformance
Share:

CORE SYSTEMS tým

Stavíme core systémy a AI agenty, které drží provoz. 15 let zkušeností s enterprise IT.