# MongoServerError: PlanExecutor error: aggregation stage 'lookup' caused memory pressure: estimated size 200MB exceeds 100MB limit

- **ID:** `mongodb/aggregation-lookup-unwind-memory-pressure`
- **Domain:** mongodb
- **Category:** resource_error
- **Verification:** ai_generated
- **Fix Rate:** 82%

## Root Cause

A $lookup stage with $unwind on large collections exceeded the 100MB memory limit for intermediate results, often due to missing indexes or cartesian product joins.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| mongodb 6.0 | active | — | — |
| mongodb 7.0 | active | — | — |
| mongodb 8.0 | active | — | — |

## Workarounds

1. **Create an index on the foreign field used in $lookup: `db.orders.createIndex({ customerId: 1 })` to reduce the size of matched documents and avoid full collection scans.** (90% success)
   ```
   Create an index on the foreign field used in $lookup: `db.orders.createIndex({ customerId: 1 })` to reduce the size of matched documents and avoid full collection scans.
   ```
2. **Restructure the pipeline: use $lookup with a pipeline to filter documents before joining, e.g., `{ $lookup: { from: 'orders', let: { custId: '$_id' }, pipeline: [ { $match: { $expr: { $eq: ['$customerId', '$$custId'] } } }, { $limit: 100 } ], as: 'orders' } }` to limit matched documents.** (85% success)
   ```
   Restructure the pipeline: use $lookup with a pipeline to filter documents before joining, e.g., `{ $lookup: { from: 'orders', let: { custId: '$_id' }, pipeline: [ { $match: { $expr: { $eq: ['$customerId', '$$custId'] } } }, { $limit: 100 } ], as: 'orders' } }` to limit matched documents.
   ```
3. **Split the aggregation: perform $lookup in a separate aggregation, write results to an intermediate collection, then run $unwind on that smaller dataset.** (80% success)
   ```
   Split the aggregation: perform $lookup in a separate aggregation, write results to an intermediate collection, then run $unwind on that smaller dataset.
   ```

## Dead Ends

- **** — allowDiskUse does not apply to intermediate results of $lookup before $unwind; it only helps for sorting and grouping stages after the join. (85% fail)
- **** — The 100MB limit is hard-coded for $lookup intermediate results and cannot be changed; attempting to set it has no effect. (95% fail)
- **** — While this may reduce memory, it changes the output structure; the root cause is the large join size, not the unwind itself. (70% fail)
