totalKeysExamined reports how many index keys MongoDB examined while executing an operation. Read it in explain("executionStats") output alongside nReturned, executionTimeMillis, and totalDocsExamined to assess whether an index limits unnecessary scanning. section 7.1.1
Interpret the metric with the winning execution stage:
- An IXSCAN plan examines index keys; when totalKeysExamined and totalDocsExamined are both 1 for one returned document, the indexed lookup is selective. section 7.1.1
- A COLLSCAN can report totalKeysExamined: 0 because MongoDB scans documents rather than index keys; totalDocsExamined can still be large. section 7.2.2
- A high key count relative to returned results indicates that the query examined many index entries before producing its result set. section 7.1.1
Inspect an indexed query
Run db.collection.find({ yourField: "value" }).explain("executionStats"). The sample plan uses the yourField_1 index, returns one document, examines one index key and one document, and completes in 3 ms. For a compound-index query, compare the same metrics after applying an index whose field order follows the query’s equality, sort, and range needs. section 7.2.2
