intermediateTop 30 Scenario-Based Questions
Your MongoDB queries are slow on a 100-million document collection. How do you diagnose and fix?
1) Identify slow queries: db.setProfilingLevel(1, 100) (log queries > 100ms). 2) Check profile: db.system.profile.find().sort({millis:-1}).limit(5). 3) For each slow query: db.collection.find({...}).explain('executionStats'). 4) Look for: COLLSCAN (no index!) and totalDocsExamined >> nReturned. 5) Add missing indexes: if query is {status:'active', createdAt:{$gt:date}}: CREATE INDEX {status:1, cre
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response