Complete the code to get the explain plan for a MongoDB query.
db.collection.find({ age: { $gt: 25 } }).[1]()The explain() method returns the query plan and execution stats for a MongoDB query.
Complete the code to get the execution statistics from the explain output.
const result = db.collection.find({ status: 'active' }).explain();
const stats = result.[1];The executionStats field contains detailed information about how the query was executed, including execution time and number of documents examined.
Fix the error in accessing the winning plan from the explain output.
const plan = db.collection.find({}).explain().[1].winningPlan;The winningPlan is inside the queryPlanner field of the explain output.
Fill both blanks to get the total number of documents examined and the number returned from execution stats.
const stats = db.collection.find({}).explain().[1];
const examined = stats.[2];The executionStats field contains totalDocsExamined which shows how many documents MongoDB looked at during the query.
Fill all three blanks to get the number of documents returned, the execution time in milliseconds, and the stage name of the winning plan.
const explain = db.collection.find({}).explain();
const returned = explain.executionStats.[1];
const timeMs = explain.executionStats.[2];
const stage = explain.queryPlanner.winningPlan.[3];nReturned is the number of documents returned, executionTimeMillis is the time taken, and stage is the name of the winning plan's root stage.