MongoDB provides an explain command to let us know how the system handles query requests. Using the explain command, we can get a good look at how the system uses the index to speed up retrieval, and we can optimize the index accordingly.
> db.t5.ensureIndex({name:1}) > db.t5.ensureIndex({age:1}) > db.t5.find({age:{$gt:45}}, {name:1}).explain() { "cursor" : "BtreeCursor age_1", "nscanned" : 0, "nscannedObjects" : 0, "n" : 0, "millis" : 0, "nYields" : 0, "nChunkSkips" : 0, "isMultiKey" : false, "indexOnly" : false, "indexBounds" : { "age" : [ [ 45, 1.7976931348623157e+308 ] ] } }Field Description:
- cursor: Returns the cursor type (BasicCursor or BtreeCursor)
- nscanned: the number of documents scanned
- n: number of documents returned
- millis: time (milliseconds)
- indexBounds: the index to use