<!-- llms-explorer concept facts · https://llms-explorer.com/tree/querystats-aggregation-stage/ · pack 2026-10-23 · ~1060 tokens -->

# $queryStats aggregation stage

> Source-anchored research for $queryStats aggregation stage. Run status: COMPLETED.

Parent: [Database Profiler vs Query Profiler](https://llms-explorer.com/tree/database-profiler-vs-query-profiler/) · 5 facets · 9 facts · page: https://llms-explorer.com/tree/querystats-aggregation-stage/

## How it works

- MongoDB's SERVER-125570 issue reports that a $queryStats stage followed by a $match on keyHash scanned the full in-memory store; its reported production case averaged about 30 seconds when filtering to roughly 100 hashes, and the ticket showed match-pushdown work in code review on 2026-10-22. (low confidence) (verified as of 2026-10-22) — [source](https://jira.mongodb.org/browse/SERVER-125570)

## Measurements and reference values

- The MongoDB 9.0 manual says $queryStats samples 1% of read and write operations by default, so the returned counts and execution times represent only sampled operations. (low confidence) (verified as of 2026-10-22) — [source](https://www.mongodb.com/docs/manual/reference/operator/aggregation/querystats/)
- MongoDB documents a query-stat store memory cap of 1% of system memory; its source README describes the store as partitioned LRU state and warns that restarts and LRU eviction reset metrics while rate limiting can skew them. (high confidence) (verified as of 2026-10-22) — [source](https://www.mongodb.com/docs/manual/reference/operator/aggregation/querystats/)
- The MongoDB 9.0 manual says $queryStats samples 1% of read and write operations by default, so its counts and execution times describe only sampled operations. Datadog's self-hosted MongoDB guide says query-stat collection is disabled by default and instructs operators on MongoDB 8.0.20 or later to set internalQueryStatsSampleRate to 1.0 (100% sampling); together, these sources show that sampling can vary with the MongoDB release and configured server parameter. (medium confidence) (verified as of 2026-10-23) — [source](https://www.mongodb.com/docs/manual/reference/operator/aggregation/querystats/)

## Problems, failure modes and limitations

- Datadog's self-hosted MongoDB DBM guide requires MongoDB 8.0 or later and says query-stat collection is disabled by default. For MongoDB 8.0.20 or later, it instructs operators to set internalQueryStatsSampleRate to 1.0 (100% sampling); for MongoDB 8.0.0 through 8.0.19, it instead instructs operators to set internalQueryStatsRateLimit to -1 (no rate limit). Treat this as integration-specific guidance and verify the deployed release and active settings. (medium confidence) (verified as of 2026-10-23) — [source](https://docs.datadoghq.com/database_monitoring/setup_mongodb/selfhosted/)
- A $queryStats pipeline must run on the admin database with $queryStats as its first stage. The stage reads the server's virtual, in-memory query-stats store and cannot be run against a specific application collection. (high confidence) — [source](https://www.mongodb.com/docs/manual/reference/operator/aggregation/querystats/)

## Comparisons and alternatives

- Run $queryStats as the first stage of a collectionless aggregation on the admin database. Its optional transformIdentifiers setting applies HMAC-SHA-256 to database, collection, and document field names, while query field values are normalized rather than stored as user data. (high confidence) — [source](https://www.mongodb.com/docs/manual/reference/operator/aggregation/querystats/)

## Facts and statements

- The stage reads an in-memory virtual query-stat store and emits aggregated runtime metrics per query-stat key; keys group similar queries by common attributes such as query shape, client, read concern, and collection type. (high confidence) — [source](https://www.mongodb.com/docs/manual/reference/operator/aggregation/querystats/)
- MongoDB warns that $queryStats is unsupported and its output format can change. The current manual says MongoDB 9.0 reorganized the output, and Percona's 9.0 review reports added insert, update, and delete coverage plus changed output serialization, so consuming tools should be version-aware. (high confidence) (verified as of 2026-10-22) — [source](https://www.mongodb.com/docs/manual/reference/operator/aggregation/querystats/)
