liveBook · Manning

Fetch stage

As explained in MongoDB 8.0 in Action, Third Edition

The FETCH stage is a MongoDB query execution stage that retrieves complete documents identified by an earlier index or record scan.

The FETCH stage retrieves complete documents after an earlier stage, commonly IXSCAN, identifies matching index keys. It appears in MongoDB execution plans between data-access stages and the final result stage. section 7.1.1

Read FETCH together with its input stage and execution statistics:

  • IXSCAN locates matching keys in an index, then passes them to FETCH for document retrieval. section 7.1.1
  • executionStats reports returned documents, execution time, keys examined, and documents examined, helping assess retrieval efficiency. section 7.1.1
  • A query that the index fully supports can avoid unnecessary collection access; otherwise, FETCH reads documents for fields not covered by the index. section 7.2.1

Inspecting FETCH with explain()

Run db.movies.find({ "runtime": 100 }).explain("executionStats"). The winning plan shows FETCH above IXSCAN on runtime_1; the statistics show 719 matching documents, 719 keys examined, and 719 documents examined in the demonstrated query. section 7.2.1

Listing 7.2 Execution-plan output structure
{
  "winningPlan": {
    "stage": "FETCH",
    "inputStage": {
      "stage": "IXSCAN",
      "indexName": "yourField_1",
      "keyPattern": {
        "yourField": 1
      },
      "indexBounds": {
        "yourField": [
          "[\"value\", \"value\"]"
        ]
      }
    }
  },
  "executionStats": {
    "nReturned": 1,
    "executionTimeMillis": 3,
    "totalKeysExamined": 1,
    "totalDocsExamined": 1
  }
}

Use the execution-plan structure to locate FETCH, inspect its IXSCAN input, and compare returned, examined, and timing metrics. section 7.1.1

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Traces FETCH within winning plans and execution statistics, while leaving slot-based engine internals as unspecified details.
Examples
Demonstrates FETCH through runtime_1 explain output, including 719 returned documents and examined keys and documents.
What it assumes
Assumes basic MongoDB queries and indexes, while demonstrating explain() output and index-supported document retrieval.
What you can do after
Diagnose query performance by reading FETCH plans and comparing keys examined, documents examined, returned results, and execution time.
How it connects
Connects FETCH with IXSCAN, COLLSCAN, executionStats, indexes, and query plan caching.

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.