liveBook · Manning

totalDocsExamined

As explained in MongoDB 8.0 in Action, Third Edition

totalDocsExamined is an explain-plan metric reporting the number of documents examined while executing a MongoDB operation.

totalDocsExamined reports how many documents MongoDB examines while executing an operation. Compare it with nReturned and totalKeysExamined to judge whether an index limits unnecessary document scanning. section 7.1.1

Interpret the metric alongside the winning execution stage:

  • An IXSCAN followed by FETCH can examine only the documents matching an indexed lookup; one returned document and totalDocsExamined: 1 indicate an efficient query. section 7.1.1
  • A COLLSCAN examines the collection when no suitable index supports the query, potentially producing a large totalDocsExamined value. section 7.2.2
  • A high ratio of examined documents to returned documents signals an opportunity to review query predicates and index design, including compound-index prefixes. section 7.2.2

Diagnosing an unsupported query

Running db.movies.find({ "imdb.rating": { $gte: 7.6 } }).explain("executionStats") produces a COLLSCAN with totalDocsExamined: 21349, totalKeysExamined: 0, and nReturned: 3472. The query examines every document because the available compound index does not begin with imdb.rating. section 7.2.2

Listing 7.1 Appending the MongoDB explain () method
db.collection.find(
    { yourField: "value" }
).explain() // Explain the default query planner information

db.collection.find(
    { yourField: "value" }
).explain(
    "executionStats" // Explain the query execution statistics
)

db.collection.find(
    { yourField: "value" }
).explain(
    "allPlansExecution" // Explain the query execution
➥plan with all possible query plans
)

Run explain("executionStats") to expose totalDocsExamined together with returned-document counts, execution time, and examined index keys. section 7.1.1

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Defines the metric and interprets indexed scans versus collection scans, but does not explain its internal implementation.
Examples
Uses one-document indexed lookup and a movies query examining 21,349 documents through COLLSCAN.
What it assumes
Assumes basic MongoDB queries and indexes, while demonstrating explain output and compound-index support limitations.
What you can do after
Diagnose inefficient queries by running explain("executionStats") and comparing examined documents with returned results.
How it connects
Connects totalDocsExamined with explain plans, executionStats, totalKeysExamined, IXSCAN, FETCH, COLLSCAN, and compound indexes.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.