How does Query.prototype.explain() work in Mongoose? - GeeksforGeeks

17 Mar, 2021
The Query.prototype.explain() function is used to set the explain option thereby making this query return detailed execution stats instead of the actual query result.
Syntax:
Query.prototype.explain()
Parameters: This function has one optional verbose parameter.Return Value: This function returns Query Object. 
Installing mongoose : 
npm install mongoose
After installing the mongoose module, you can check your mongoose version in command prompt using the command. 
npm mongoose --version
Now, create a folder and add a file for example, index.js as shown below.
Database: The sample database used here is shown below:  
Project Structure: The project structure will look like this.
Example 1:
index.js
const mongoose = require('mongoose');  // Database connectionmongoose.connect('mongodb://127.0.0.1:27017/geeksforgeeks', {    useNewUrlParser: true,    useCreateIndex: true,    useUnifiedTopology: true});  // User modelconst User = mongoose.model('User', {     name: { type: String },    age: { type: Number }});  var query = User.find({age: 5}).explain('queryPlanner')query.exec(function(err, res){    if(err) console.log(err.message)    else console.log(res)});
Run index.js file using below command:
node index.js
Output : 
[
  {
    queryPlanner: {
      plannerVersion: 1,
      namespace: 'geeksforgeeks.users',
      indexFilterSet: false,
      parsedQuery: [Object],
      queryHash: '3838C5A3',
      planCacheKey: '38305F3',
      winningPlan: [Object],
      rejectedPlans: []
    },
    executionStats: {
      executionSuccess: true,
      nReturned: 1,
      executionTimeMillis: 0,
      totalKeysExamined: 0,
      totalDocsExamined: 4,
      executionStages: [Object],
      allPlansExecution: []
    },
    serverInfo: {
      host: 'Lenovo530S',
      port: 27017,
      version: '4.2.0',
      gitVersion: 'a4b751dcf51dd249c58812b390cfd1c0129c30'
    },
    ok: 1
  }
]
Example 2:
index.js
const express = require('express');const mongoose = require('mongoose');const app = express()  // Database connectionmongoose.connect('mongodb://127.0.0.1:27017/geeksforgeeks', {    useNewUrlParser: true,    useCreateIndex: true,    useUnifiedTopology: true});  // User modelconst User = mongoose.model('User', {     name: { type: String },    age: { type: Number }});  var query = User.find({age: 5}).explain('allPlansExecution')query.exec(function(err, res){    if(err) console.log(err.message)    else console.log(res)});  app.listen(3000, function(error ) {    if(error) console.log(error)    console.log("Server listening on PORT 3000")});
Run index.js file using below command:
node index.js
Output : 
Server listening on PORT 3000
[
  {
    queryPlanner: {
      plannerVersion: 1,
      namespace: 'geeksforgeeks.users',
      indexFilterSet: false,
      parsedQuery: [Object],
      queryHash: '3838SF3',
      planCacheKey: '3238C5F3',
      winningPlan: [Object],
      rejectedPlans: []
    },
    executionStats: {
      executionSuccess: true,
      nReturned: 1,
      executionTimeMillis: 0,
      totalKeysExamined: 0,
      totalDocsExamined: 4,
      executionStages: [Object],
      allPlansExecution: []
    },
    serverInfo: {
      host: 'Lenovo530S',
      port: 27017,
      version: '4.2.0',
      gitVersion: 'a4b751dcf51sd249c5865812b390cfd1c0129c30'
    },
    ok: 1
  }
]
Reference: https://mongoosejs.com/docs/api/query.html#query_Query-explain
Mongoose
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Express.js express.Router() Function
Mongoose Populate() Method
JWT Authentication with Node.js
Node.js Event Loop
How to build a basic CRUD app with Node.js and ReactJS ?
Express.js express.Router() Function
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React