# MongoServerError: $lookup with 'from' referencing a view is not supported in this version

- **ID:** `mongodb/aggregation-lookup-views-not-supported`
- **Domain:** mongodb
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 85%

## Root Cause

The aggregation pipeline uses $lookup to join with a view, which is not allowed in MongoDB versions prior to 5.0 or in certain configurations.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| MongoDB 4.4 | active | — | — |
| MongoDB 5.0 | active | — | — |
| MongoDB 6.0 | active | — | — |

## Workarounds

1. **Replace the $lookup on a view with a $lookup on the underlying collection, then filter using $match: db.orders.aggregate([ { $lookup: { from: "products", localField: "productId", foreignField: "_id", as: "product" } }, { $match: { "product.price": { $gt: 100 } } } ])** (90% success)
   ```
   Replace the $lookup on a view with a $lookup on the underlying collection, then filter using $match: db.orders.aggregate([ { $lookup: { from: "products", localField: "productId", foreignField: "_id", as: "product" } }, { $match: { "product.price": { $gt: 100 } } } ])
   ```
2. **Materialize the view as a real collection using $merge: db.sourceCollection.aggregate([ { $match: ... }, { $merge: { into: "materializedView" } } ]); then use $lookup on the materialized collection.** (85% success)
   ```
   Materialize the view as a real collection using $merge: db.sourceCollection.aggregate([ { $match: ... }, { $merge: { into: "materializedView" } } ]); then use $lookup on the materialized collection.
   ```
3. **If using MongoDB 5.0+, ensure the feature flag for views in $lookup is enabled by checking the version and using the correct syntax: db.adminCommand({ getParameter: 1, featureCompatibilityVersion: 1 })** (80% success)
   ```
   If using MongoDB 5.0+, ensure the feature flag for views in $lookup is enabled by checking the version and using the correct syntax: db.adminCommand({ getParameter: 1, featureCompatibilityVersion: 1 })
   ```

## Dead Ends

- **** — Even in supported versions, $lookup on views has limitations (e.g., no $lookup on sharded views). (60% fail)
- **** — Indexes on views are not supported; the underlying collection must be indexed. (95% fail)
- **** — The error is about referencing a view, not about the syntax used. (80% fail)
