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
