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 prefixdb.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$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.
No index on the hot filter/sort, or an index on every field. A compound whose left key is not in the query. A boolean-only index. Never dropping unused indexes. I would explain('executionStats') and build one ESR compound for that shape.
Practice