# MongoServerError：变更流恢复令牌对命名空间 'mydb.mycoll' 无效：令牌属于不同的数据库

- **ID:** `mongodb/change-stream-resume-token-invalid-namespace`
- **领域:** mongodb
- **类别:** protocol_error
- **验证级别:** ai_generated
- **修复率:** 87%

## 根因

提供的恢复令牌用于打开变更流，但该令牌来自与被监视的数据库或集合不同的命名空间，导致命名空间不匹配。

## 解决方案

1. ```
   Open a new change stream without a resume token (e.g., db.collection.watch([], {startAtOperationTime: Timestamp(1, 1)})) to start from the earliest possible point, then capture a fresh token for future resumption.
   ```
2. ```
   If the token is from a different namespace, construct a new change stream on the correct namespace using db.collection.watch() and ignore the old token. Example in Node.js: const changeStream = collection.watch(); changeStream.on('change', (change) => { console.log(change._id); });
   ```

## 无效尝试

- **** — Reusing a resume token from a different replica set or sharded cluster (e.g., from production in a test environment) still fails because the token is cluster-specific. (90% 失败率)
- **** — Manually editing the resume token's namespace field in the application code does not work because the server validates the token against internal metadata. (95% 失败率)
