# MongoServerError: 变更流管道必须是有效的 JSON 数组：位置 10 处无效的 JSON 输入

- **ID:** `mongodb/change-stream-invalid-json`
- **领域:** mongodb
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 95%

## 根因

变更流管道参数作为无效的 JSON 字符串或格式错误的数组传递，通常是由于应用程序代码中的字符串拼接错误。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| MongoDB 5.0 | active | — | — |
| MongoDB 6.0 | active | — | — |
| MongoDB 7.0 | active | — | — |

## 解决方案

1. ```
   确保管道作为原生数组对象传递，而不是 JSON 字符串。在 Node.js 中：db.collection.watch([{$match: {operationType: 'insert'}}])。在 Python 中：db.collection.watch([{'$match': {'operationType': 'insert'}}])。
   ```
2. ```
   在将管道传递给 watch 方法之前，使用 JavaScript 中的 JSON.parse() 或 Python 中的 json.loads() 验证管道 JSON。
   ```

## 无效尝试

- **** — Adding extra quotes around the pipeline array does not fix the JSON parsing; it causes additional syntax errors. (90% 失败率)
- **** — Using a string instead of an array for the pipeline (e.g., '[{"$match": {}}]') is parsed as a string literal, not an array. (95% 失败率)
