<!-- llms-explorer concept facts · https://llms-explorer.com/tree/pinned-compass-preview-cap-injection-and-collection-count-guard/ · pack 2026-11-02 · ~1533 tokens -->

# Pinned Compass preview cap injection and collection-count guard

> Source-anchored research for Pinned Compass preview cap injection and collection-count guard. Run status: COMPLETED.

Parent: [Compass aggregation preview sampling versus full pipeline execution](https://llms-explorer.com/tree/compass-aggregation-preview-sampling-versus-full-pipeline-execution/) · 3 facets · 6 facts · page: https://llms-explorer.com/tree/pinned-compass-preview-cap-injection-and-collection-count-guard/

## Problems, failure modes and limitations

- In the cited pinned implementation, `createPreviewAggregation` permits added input limits when `options.totalDocumentCount` is falsy or exceeds the effective sample size computed from `options.sampleSize` and `DEFAULT_SAMPLE_SIZE`. A truthy count at or below that size suppresses those input limits; the fixture with `totalDocumentCount: 10` retains `$group` and `$bucket` without prepended limits while retaining the final preview limit. An omitted or zero count permits insertion before qualifying full-scan stages, including when a zero count equals an effective sample size of zero. The zero-count boundary is a deduction from the shown predicate. (high confidence) — [source](https://raw.githubusercontent.com/mongodb-js/compass/1292483d33fe97513c41e0e67020e4438deb3f69/packages/compass-aggregations/src/modules/pipeline-builder/pipeline-preview-manager.ts) · [also](https://raw.githubusercontent.com/mongodb-js/compass/1292483d33fe97513c41e0e67020e4438deb3f69/packages/compass-aggregations/src/modules/pipeline-builder/pipeline-preview-manager.spec.ts)
- In the cited pinned implementation, `createPreviewAggregation` loops over the supplied top-level stages and, when its collection-count predicate allows it, inserts a separate `$limit` immediately before each operator classified in `FULL_SCAN_OPS`. The source fixture for `$group` followed by `$bucket` asserts an input limit before each stage, then a final preview limit; these are source assertions, not tests executed for this research. (high confidence) — [source](https://raw.githubusercontent.com/mongodb-js/compass/1292483d33fe97513c41e0e67020e4438deb3f69/packages/compass-aggregations/src/modules/pipeline-builder/pipeline-preview-manager.ts) · [also](https://raw.githubusercontent.com/mongodb-js/compass/1292483d33fe97513c41e0e67020e4438deb3f69/packages/compass-aggregations/src/modules/pipeline-builder/pipeline-preview-manager.spec.ts) · [also](https://raw.githubusercontent.com/mongodb-js/compass/1292483d33fe97513c41e0e67020e4438deb3f69/packages/compass-aggregations/src/utils/stage.ts)
- The final preview-limit guard is independent of the collection-count guard. It appends `$limit: options.previewSize ?? DEFAULT_PREVIEW_LIMIT` unless the final constructed stage operator belongs to `REQUIRED_AS_FIRST_STAGE_OPS`. The index-statistics fixture for a sole stage classified as required first expects that stage alone, while the empty-pipeline fixture expects the final limit. (high confidence) — [source](https://raw.githubusercontent.com/mongodb-js/compass/1292483d33fe97513c41e0e67020e4438deb3f69/packages/compass-aggregations/src/modules/pipeline-builder/pipeline-preview-manager.ts) · [also](https://raw.githubusercontent.com/mongodb-js/compass/1292483d33fe97513c41e0e67020e4438deb3f69/packages/compass-aggregations/src/modules/pipeline-builder/pipeline-preview-manager.spec.ts) · [also](https://raw.githubusercontent.com/mongodb-js/compass/1292483d33fe97513c41e0e67020e4438deb3f69/packages/compass-aggregations/src/utils/stage.ts)
- The helper rejects a pipeline when `isLastStageOutputStage(pipeline)` is true, before constructing preview stages. Its fixture expects a pipeline ending in `$out` to throw. A separate fixture explicitly expects a non-final `$out` followed by `$sort` to pass through this helper and receive the final preview limit; that assertion establishes helper transformation behavior, not server acceptance or safe execution of that pipeline. (medium confidence) — [source](https://raw.githubusercontent.com/mongodb-js/compass/1292483d33fe97513c41e0e67020e4438deb3f69/packages/compass-aggregations/src/modules/pipeline-builder/pipeline-preview-manager.ts) · [also](https://raw.githubusercontent.com/mongodb-js/compass/1292483d33fe97513c41e0e67020e4438deb3f69/packages/compass-aggregations/src/modules/pipeline-builder/pipeline-preview-manager.spec.ts)

## Comparisons and alternatives

- Input limits use `options.sampleSize ?? DEFAULT_SAMPLE_SIZE`; the appended limit independently uses `options.previewSize ?? DEFAULT_PREVIEW_LIMIT`. These nullish fallbacks preserve explicitly supplied zero values rather than replacing them by truthiness, and the helper contains no option-range validation. This describes transformation code only, not server acceptance of those values. This helper reads the supplied collection count rather than querying it. (low confidence) — [source](https://raw.githubusercontent.com/mongodb-js/compass/1292483d33fe97513c41e0e67020e4438deb3f69/packages/compass-aggregations/src/modules/pipeline-builder/pipeline-preview-manager.ts)

## Facts and statements

- In the pinned stage utilities, `getStageOperator` returns the first key of `stage ?? {}`. `getLastStageOperator` inspects the final array element and substitutes an empty string for an absent operator. `isLastStageOutputStage` applies `isOutputStage` only to that final operator, and `isOutputStage` checks the `OUT_OPERATOR_NAMES` set. This file imports `OUT_STAGES` from `@mongodb-js/mongodb-constants` and constructs that set from each entry's `stage.value`. The preview helper uses these operator helpers for its injection and final-stage guards. These helper bodies inspect top-level keys without recursively inspecting nested pipelines. (medium confidence) — [source](https://raw.githubusercontent.com/mongodb-js/compass/1292483d33fe97513c41e0e67020e4438deb3f69/packages/compass-aggregations/src/modules/pipeline-builder/pipeline-preview-manager.ts) · [also](https://raw.githubusercontent.com/mongodb-js/compass/1292483d33fe97513c41e0e67020e4438deb3f69/packages/compass-aggregations/src/utils/stage.ts)
