liveBook · Manning

COLLSCAN

As explained in MongoDB 8.0 in Action, Third Edition

COLLSCAN is a MongoDB query-plan stage that examines documents by scanning an entire collection rather than using an index.

COLLSCAN is a MongoDB query-plan stage that examines every document in a collection instead of using an index. It can be acceptable for small collections but signals possible inefficiency as data grows. section 7.1.1 section 7.2.2

Use execution statistics to assess the scan:

  • Compare totalDocsExamined with nReturned to see how many documents MongoDB inspected to produce the results. section 7.2.2
  • A query using COLLSCAN examines no index keys, unlike an IXSCAN followed by FETCH. section 7.1.1 section 7.2.2
  • Review compound-index prefixes when a query omits the index's leading field; such queries may fall back to COLLSCAN. section 7.2.2

Inspecting a collection scan

Run db.movies.find({ "imdb.rating": { $gte: 7.6 } }).explain("executionStats"). The winning plan reports COLLSCAN, nReturned: 3472, totalKeysExamined: 0, and totalDocsExamined: 21349, showing that MongoDB scanned the entire movies collection. 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
)

Compare default planner information, execution statistics, and all possible query plans with explain(). section 7.1.1

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Defines the stage and interprets execution statistics through a worked query plan, without detailing query-engine internals.
Examples
Uses a movies query filtering IMDb ratings and reports 3,472 results after examining 21,349 documents.
What it assumes
Assumes MongoDB find queries and indexes while teaching how compound-index coverage affects the winning plan.
What you can do after
Diagnose/debug with explain() by identifying full scans and comparing examined documents, returned results, and index keys.
How it connects
Connects collection scans with indexes, compound-index prefixes, IXSCAN, FETCH, and explain execution statistics.

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.