What is the MongoDB profiler?
Why Interviewers Ask This
This tests whether you can apply MongoDB knowledge to real-world scenarios. Interviewers are looking for clarity of thought and evidence that you've encountered this in production code.
Answer
The MongoDB profiler collects data about database operations — queries, writes, aggregations — to help identify slow queries and performance bottlenecks. It stores profiling data in the system.profile capped collection in each database. Profiling levels: 0 — off (default); 1 — slow operations only (those exceeding slowms threshold, default 100ms); 2 — all operations. Setting the profiler: db.setProfilingLevel(1, { slowms: 50 }) — capture queries taking more than 50ms. Viewing profiled queries: db.system.profile.find().sort({ millis: -1 }).limit(10) — slowest 10 operations. Profile document fields: op (operation type: query, insert, update, delete, command), ns (namespace), millis (execution time), planSummary (IXSCAN/COLLSCAN), nreturned, keysExamined, docsExamined, ts (timestamp), query (the filter). Finding slow queries: look for COLLSCAN (collection scan) in planSummary or high docsExamined/nreturned ratios. Atlas Performance Advisor: in MongoDB Atlas, the Performance Advisor automatically analyzes queries and suggests indexes — much more convenient than manual profiling. currentOp(): db.currentOp() shows in-progress operations — useful for finding long-running operations or locks. db.killOp(opId) to kill a running operation.
Pro Tip
If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.