Skip to content

Common Mistakes

Index Mistakes

Missing the index that matters, or creating so many that writes stall. Problem, bad example, better.

IntermediateAbout 5 minutes

Problem. The hot query is slow, or writes are slow. Usually both: no useful index, or an index on every field.

db.users.createIndex({ isActive: 1 })
db.orders.createIndex({ status: 1, userId: 1 })

db.orders.find({ userId }).sort({ createdAt: -1 })  // not a left prefix
Bad — boolean index, and a compound that cannot serve the query
db.orders.find({ userId }).sort({ createdAt: -1 }).explain("executionStats")
db.orders.createIndex({ userId: 1, createdAt: -1 })
db.users.createIndex({ city: 1 })  // selective equality, not isActive alone
Better — explain first, one ESR compound per hot shape

$indexStats, then drop unused trees. Two indexes that share the same left prefix are one too many. explain examined ≫ returned is the argument, not 'we should add indexes'.

Interview question

What index mistakes show up most in code review?

Think about it first.

Practice