在 MongoDB 4.4 中使用 $function 处理 JSON 字符串时,出现 'Error compiling expression: Unexpected end of input' 错误的原因可能是参数传递不正确。在你的代码中,args 参数应该是一个数组,其中包含传递给函数的参数。

在你的代码中,args 参数是一个字符串数组,包含一个元素 '$eventInfo'。这会导致函数在解析时出错,因为它期望一个有效的 JSON 字符串,而不是一个字符串数组。

为了解决这个问题,你应该将 args 参数设置为一个包含字符串 '$eventInfo' 的数组,如下所示:

db.collection('ceshi').aggregate([
  {
    $project: {
      str: {
        $function: {
          body: function(str) {
            return JSON.parse(str);
          },
          args: ['$eventInfo'], // 修正此处参数为数组形式
          lang: "js"
        }
      }
    }
  }
]);

通过将 args 参数设置为包含 '$eventInfo' 的数组,你可以解决 'Error compiling expression: Unexpected end of input' 错误。

MongoDB 4.4 中 $function 处理 JSON 字符串报错:

原文地址: https://www.cveoy.top/t/topic/p7vY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录