---
name: "mongodb-aggregation-pipeline-stages"
description: "Construct multi-stage analytical aggregation pipelines in MongoDB with high index selectivity."
metadata:
  author: "MongoDB"
  version: "1.0.0"
  runtime_compatibility:
    - "codex"
    - "claude-code"
    - "cursor"
    - "windsurf"
triggers:
  - "optimize mongodb aggregation"
  - "use mongodb lookup and group"
  - "explain mongodb aggregate pipeline"
permissions:
  fs_read: true
  fs_write: "workspace"
  shell_exec: false
  net_access: false
---

# MongoDB Aggregation Framework Pipeline Stages

Designs MongoDB aggregation queries: optimizing $match, $lookup with sub-pipelines, $unwind, $group with accumulators, $facet for multi-dimensional dashboards, and explain plans with executionStats.

## When to Invoke
Invoke this skill whenever you need to:
- optimize mongodb aggregation
- use mongodb lookup and group
- explain mongodb aggregate pipeline

## Execution SOP & Rules
1. Always ensure $match precedes $lookup and $group to leverage compound indexes and minimize working set.
2. Use $project or $set to prune unused heavy embedded document arrays early in the pipeline.
3. Check execution metrics using db.collection.explain('executionStats').aggregate([...]).
4. Add allowDiskUse: true when processing intermediate datasets exceeding the 100MB RAM pipeline limit.

## Verification Checklist
- Run local linting, syntax validation, and type-checks before reporting completion.
- Ensure all created or modified files adhere to workspace boundaries and avoid uncommitted secrets.
- Verify tests or build commands exit cleanly with return code 0.
