Index Types in MongoDB

From the MongoDB cheat sheet · Indexes & Performance · verified Jul 2026

Index Types

Different index types for various use cases

javascript
// Single field index
db.users.createIndex({ email: 1 })  // 1 = ascending
db.posts.createIndex({ createdAt: -1 })  // -1 = descending

// Compound index (multiple fields)
db.orders.createIndex({ 
  customerId: 1, 
  createdAt: -1 
})

// Unique index
db.users.createIndex(
  { email: 1 }, 
  { unique: true }
)

// Text index for search
db.articles.createIndex({ 
  title: "text", 
  content: "text" 
})
🟢 Essential - Indexes make queries fast
💡 Compound indexes support multiple query patterns
⚠️ Too many indexes slow down writes
📌 Index order matters in compound indexes
⚡ Use explain() to verify index usage
indexesperformance

Continue with MongoDB

Save the full cheat sheet or work through every related task.

More MongoDB tasks

Back to the full MongoDB cheat sheet