db.inventory.createIndex( { quantity: 1 } ) 

To view the query plan statistics, use the explain("executionStats") method: 

db.inventory.find( { quantity: { $gte: 100, $lte: 200 } } ).explain("executionStats") 

The explain() method returns the following results: 

{ "queryPlanner" : { "plannerVersion" : 1, ... "winningPlan" : { "stage" : "FETCH", "inputStage" : { "stage" : "IXSCAN", "keyPattern" : { "quantity" : 1 }, ... } }, "rejectedPlans" : [ ] }, "executionStats" : { "executionSuccess" : true , "nReturned" : 3, "executionTimeMillis" : 0, "totalKeysExamined" : 3, "totalDocsExamined" : 3, "executionStages" : { ... }, ... }, ... } 

• queryPlanner.winningPlan.inputStage.stage displays IXSCAN to indicate index use. 

• executionStats.nReturned displays 3 to indicate that the query matches and returns three documents. 

• executionStats.totalKeysExamined display 3 to indicate that MongoDB scanned three index en- tries. 

• executionStats.totalDocsExamined display 3 to indicate that MongoDB scanned three documents. 

When run with an index, the query scanned 3 index entries and 3 documents to return 3 matching documents. Without the index, to return the 3 matching documents, the query had to scan the whole collection, scanning 10 documents. 

## Compare Performance of Indexes 

To manually compare the performance of a query using more than one index, you can use the hint() method in conjunction with the explain() method. 

Consider the following query: 

