Blog

Find the MongoDB Query Scanning Your Collection

Find a scan in WatchLog, compare examined and returned documents, and confirm the MongoDB plan before changing an index.

MongoDB operation drawer in WatchLog showing the shop.events query shape, a COLLSCAN plan and the ratio of documents examined to results.
WatchLog’s MongoDB operation drawer.

MongoDB can return a few results and still read most of a collection to find them. A latency spike tells you when to look. The operation shape and query plan tell you whether the read is doing more work than the application expects.

Start with the operation, not the database average

Open the MongoDB instance in WatchLog. If Right now flags a query reading a whole collection, follow it into the operation details. Otherwise open Operations and look for the repeated shape that lines up with the latency increase. Note the database, collection, operation and its filter or sort. A shape groups similar operations without the literal query values. A deploy marker narrows down when to look; it does not prove what caused the scan.

Compare documents examined with documents returned

Open the operation drawer. Examining many documents to return very few is a reason to investigate, not proof of a collection scan. A scan on a small collection or an intentionally broad query may be reasonable. If WatchLog names a likely sending workload, use that as a lead and check its code for the same filter and sort. WatchLog’s example shows a shape on shop.events reading about 3 million documents per call.

Confirm the query plan before changing an index

On representative test data, run MongoDB’s explain for a read query with the same filter and sort. Look for a COLLSCAN stage in the winning plan. To compare totalDocsExamined and nReturned, use executionStats on a suitable test workload: it executes the query, so avoid running a heavy reproduction blindly on a busy primary. Treat any index suggested by the drawer as a candidate. Compare the before and after plans, and account for write and storage costs before making it permanent.

Check where WatchLog got the evidence

WatchLog’s setup uses a clusterMonitor user. Operations prefers $queryStats when it has usable rows; otherwise shapes can come from MongoDB’s slow log and periodic samples of running operations. Those fallback sources do not show every fast query or reliably link every operation to a service. Check the source shown in the page. A missing shape or separate service and collection lists mean you have less evidence, not that the query is healthy.

See WatchLog’s MongoDB example, monitoring setup and data sources, and MongoDB’s guide to interpreting an explain plan.

See your own servers in five minutes.

Free on WatchLog Cloud for 5 servers or nodes, or run it yourself, open source.