Performance Tuning — MongoDB Technical Interview Questions & Answers

explain(), indexes and the profiler.

Performance Tuning

  • Simple meaning: Make finds fast with the right index, a tight filter, and explain().
  • How to use: .explain("executionStats") — want IXSCAN. Profiler: db.setProfilingLevel(1, { slowms: 100 }).
  • Also: projection so you don’t fetch huge arrays. $match early in aggregate. Avoid huge skip.
  • Common mistake: adding 15 indexes “just in case”.
  • Interview tip: Measure with explain. Index the query. Don’t guess.

Technical Interview Tip: explain → index → re-explain.

Code Examples for Performance Tuning

Performance Tuning — working example

db.users.find({ email: "ada@example.com" }).explain("executionStats")
db.setProfilingLevel(1, { slowms: 100 })
db.system.profile.find().sort({ ts: -1 }).limit(3)

Performance Tuning — explain() shows the plan; the profiler records slow operations for tuning.

How to Explain Performance Tuning to an Interviewer

How do you check if a query uses an index?

explain("executionStats") Look for IXSCAN