mongodb config_error ai_generated true

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

MongoServerError: Change stream pipeline must be a valid JSON array: invalid JSON input at position 10

ID: mongodb/change-stream-invalid-json

其他格式: JSON · Markdown 中文 · English
95%修复率
90%置信度
1证据数
2024-04-18首次发现

版本兼容性

版本状态引入弃用备注
MongoDB 5.0 active
MongoDB 6.0 active
MongoDB 7.0 active

根因分析

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

English

The change stream pipeline argument was passed as an invalid JSON string or malformed array, often due to string concatenation errors in application code.

generic

官方文档

https://www.mongodb.com/docs/manual/changeStreams/

解决方案

  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。

无效尝试

常见但无效的做法:

  1. 90% 失败

    Adding extra quotes around the pipeline array does not fix the JSON parsing; it causes additional syntax errors.

  2. 95% 失败

    Using a string instead of an array for the pipeline (e.g., '[{"$match": {}}]') is parsed as a string literal, not an array.