# MongoServerError: change stream namespace not found

- **ID:** `mongodb/change-stream-namespace-not-found`
- **Domain:** mongodb
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

The collection or database specified in the change stream pipeline does not exist or was dropped during the stream's lifetime.

## Version Compatibility

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

## Workarounds

1. **Verify the collection exists before opening the change stream: db.collection('myColl').exists() then create if missing.** (85% success)
   ```
   Verify the collection exists before opening the change stream: db.collection('myColl').exists() then create if missing.
   ```
2. **Use a try-catch block to recreate the change stream when this error occurs: try { cursor = collection.watch(); } catch (e) { if (e.code === 166) { db.createCollection('myColl'); } }** (75% success)
   ```
   Use a try-catch block to recreate the change stream when this error occurs: try { cursor = collection.watch(); } catch (e) { if (e.code === 166) { db.createCollection('myColl'); } }
   ```

## Dead Ends

- **** — The collection still doesn't exist, so the error persists. (80% fail)
- **** — Batch size does not affect namespace validity; the root cause is missing collection. (90% fail)
