analyze our mongoDB queries using explain() method

You are here : Home / MongoDB Tutorial



Contents of page >
  • 1) About explain() methods in MongoDB >
  • 2) Let's create new collection and insert document in it  in MongoDB>
  • 3) Creating Covered Index before using explain() method in MongoDB - (create compound index on field firstName and salary) >
  • 4) Now, now let's query/read data using explain() method in MongoDB >
  • 5) Summary -


1) About explain() methods in MongoDB >
  • It helps in finding indexes in query.

  • It helps in finding whether our query has used index or not.

  • How many number of documents were scanned.
  • How many number of indexes were scanned.
  • It provides server info.

2) Let's create new collection and insert document in it  in MongoDB>
> db.employee.insert({firstName:"Ankit", lastName:"Mittal", salary : 1000 })
> db.employee.insert({firstName:"Neha", lastName:"Kati", salary : 2000 })
Above will create collection (or table) (if collection already exists it will insert documents in it).

3) Creating Covered Index before using explain() method in MongoDB - (create compound index on field firstName and salary)>
> db.employee.ensureIndex({firstName  : 1}, {salary : 1} )




4) Now, now let's query/read data using explain() method in MongoDB >

db.employee.find({firstName:"Ankit"},{salary:1, _id :0}).explain()

In this query output below we can see that covered index was used.

Output>
> db.employee.find({firstName:"Ankit"},{salary:1, _id :0}).explain()
{
       "queryPlanner" : {
               "plannerVersion" : 1,
               "namespace" : "mydb.employee",
               "indexFilterSet" : false,
               "parsedQuery" : {
                       "firstName" : {
                               "$eq" : "Ankit"
                       }
               },
               "winningPlan" : {
                       "stage" : "PROJECTION",
                       "transformBy" : {
                               "salary" : 1,
                               "_id" : 0
                       },
                       "inputStage" : {
                               "stage" : "FETCH",
                               "inputStage" : {
                                       "stage" : "IXSCAN",
                                       "keyPattern" : {
                                               "firstName" : 1
                                       },
                                       "indexName" : "firstName_1",
                                       "isMultiKey" : false,
                                       "direction" : "forward",
                                       "indexBounds" : {
                                               "firstName" : [
                                                       "[\"Ankit\", \"Ankit\"]"
                                               ]
                                       }
                               }
                       }
               },
               "rejectedPlans" : [ ]
       },
       "serverInfo" : {
               "host" : "ankitMittal01",
               "port" : 27017,
               "version" : "3.0.6",
               "gitVersion" : "1ef45a23a4c5e3480ac919b28afcba3c615488f2"
       },
       "ok" : 1
}


  • The true value of indexOnly indicates that this query has used indexing.
  • The cursor field specifies the type of cursor used. BTreeCursor type indicates that an index was used and also gives the name of the index used. BasicCursor indicates that a full scan was made without using any indexes.
  • n indicates the number of documents matching returned.
  • nscannedObjects indicates the total number of documents scanned.
  • nscanned indicates the total number of documents or index entries scanned.


5) Summary -
So in this MongoDB tutorial we learned how to analyze our mongoDB queries using explain() method.

Let's create new collection and insert document in it >
> db.employee.insert({firstName:"Ankit", lastName:"Mittal", salary : 1000 })

Creating Covered Index before using explain() method -
> db.employee.ensureIndex({firstName  : 1}, {salary : 1} )

Now, now let's query using explain() method in MongoDB >
db.employee.find({firstName:"Ankit"},{salary:1, _id :0}).explain()



Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter. You may join our fbGroup or linkedInGroup as well.



RELATED LINKS>

What is MongoDB - A quick introduction to database


Labels: MongoDB
eEdit
Must read for you :