Skip to content
Related Articles

Related Articles

Improve Article

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

  • Last Updated : 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 connection
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true
});
  
// User model
const 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 connection
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true
});
  
// User model
const 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




My Personal Notes arrow_drop_up
Recommended Articles
Page :