<!-- llms-explorer concept facts · https://llms-explorer.com/tree/count-scan-covered-counts/ · pack 2026-10-21 · ~1048 tokens -->

# COUNT_SCAN covered counts

> Source-anchored research for COUNT_SCAN covered counts. Run status: COMPLETED.

Parent: [MongoDB Query Shape Optimization: Sort, Pagination, Operators and Sharded Targeting](https://llms-explorer.com/tree/mongodb-query-shape-optimization-sort-pagination-operators-and-sharded-targeting/) · 6 facets · 9 facts · page: https://llms-explorer.com/tree/count-scan-covered-counts/

## Definitions

- In MongoDB's classic execution tree, COUNT is the root stage that counts its child's results, while COUNT_SCAN is a distinct, specialized index-access stage that can optimize some count operations. (high confidence) — [source](https://raw.githubusercontent.com/mongodb/mongo/master/src/mongo/db/exec/classic/count.h)

## How it works

- For the documented db.collection.count() path, MongoDB can count from index entries alone when an index supports the query, every predicate field is in that index, and the predicates bound one contiguous index-key range. Predicates on fields outside the index or multiple disjoint ranges require document reads. (high confidence) — [source](https://www.mongodb.com/docs/manual/reference/method/db.collection.count.md)

## How-to and procedures

- Use explain("executionStats") to verify a candidate: a covered count should examine zero documents, while totalKeysExamined reports index entries scanned. A COUNT_SCAN stage is consistent with an index-only count, but a plan containing IXSCAN should also be checked for FETCH and nonzero totalDocsExamined. (high confidence) — [source](https://www.mongodb.com/docs/manual/reference/explain-results/)

## Problems, failure modes and limitations

- Explain reports totalDocsExamined: 0 when an index-only access avoids examining collection documents, but totalKeysExamined still measures index entries. MongoDB's CountScan implementation advances an index cursor and increments keysExamined, so a large covered count can remain slow even when it fetches no documents. (medium confidence) — [source](https://www.mongodb.com/docs/manual/reference/explain-results/)

## Comparisons and alternatives

- countDocuments() returns an accurate count by running an aggregation with $match and $group/$sum rather than reading collection metadata; its plan can use an index, but the actual explain plan must be checked. (high confidence) — [source](https://www.mongodb.com/docs/manual/reference/method/db.collection.countDocuments/)
- MongoDB's query optimization IDL sets internalQueryPlannerEnableCountScanForUnfilteredCount to true by default and permits COUNT_SCAN on the _id index for count requests with no predicate instead of COLLSCAN; the knob's minimum FCV is 9.0. (low confidence) (verified as of 2026-10-20) — [source](https://github.com/mongodb/mongo/blob/35647d07bc4bc11966c90fe7100eb1db0b0ac1d9/src/mongo/db/query/query_optimization_knobs.idl)

## Facts and statements

- In MongoDB's sharding test, equality on the full shard key allows a count plan to omit SHARDING_FILTER and use COUNT_SCAN. The tested partial compound key, $in shard-key predicate, and unrelated-field cases retain SHARDING_FILTER. (low confidence) (verified as of 2026-10-20) — [source](https://raw.githubusercontent.com/mongodb/mongo/master/jstests/sharding/query/queries_elide_shard_filter.js)
- MongoDB's CountScan executor iterates an index cursor between start and end keys, and MongoDB's queryStats taxonomy describes countScan as counting index keys without returning documents. Microsoft describes the same broad COUNT_SCAN behavior in Azure DocumentDB's explain output; that is corroboration from a Mongo-compatible product, not evidence that its planner choices match MongoDB's. (high confidence) — [source](https://raw.githubusercontent.com/mongodb/mongo/master/src/mongo/db/exec/classic/count_scan.cpp)
- If count predicates span disjoint index ranges or include a field outside the chosen index, MongoDB may use the index and still read documents to finish the count. In explain output, FETCH is a document-reading stage, so inspect the plan tree as well as the fact that an index appears. (high confidence) — [source](https://www.mongodb.com/docs/manual/reference/method/db.collection.count.md)
