简体   繁体   English

在 MongoDB 中使用 $function new 运算符进行 updateMany 失败

[英]using $function new operator in MongoDB for updateMany failed

I have an array tree and I want to update that array in my collection with this syntax我有一个数组树,我想用这个语法更新我的集合中的数组

db.documents.updateMany(
  {
    companyId: ObjectId("5f29074048538b6403bc71ab",
    tree: {
      $in: [ObjectId("5f539bf696fa1748fa417b07")],
    },
  },
  [{
    $set: {
      tree: {
        $function: {
          body:
            "function(t) { t.push(ObjectId('5f5494a1d708bda4988d75d4')); return t.filter(v => [ObjectId('5f539bf696fa1748fa417b07')].includes(v))}}",
          args: ["$tree"],
          lang: "js",
        },
      },
    },
  }]
);

why I write body function with string?为什么我用字符串写body函数? because in Golang I can't use code for bson, so I must use function string, the syntax above is I run on command line mongo cli to try there first then implement to my Go app因为在 Golang 中我不能使用 bson 的代码,所以我必须使用函数字符串,上面的语法是我在命令行 mongo cli 上运行首先尝试然后实现到我的 Go 应用程序

the error I got is我得到的错误是

Unrecognized expression '$function'

anyone has same problem ?有人有同样的问题吗? or I did wrong to use $function operator ??还是我使用$function操作符做错了??

I had this problem too.我也有这个问题。 In my scenario, the problem was the version of MongoDB instance.在我的场景中,问题是 MongoDB 实例的版本。 Check the documentations of $function https://docs.mongodb.com/manual/reference/operator/aggregation/function/ , it says the functions was introduced in version 4.4 If you any doubts about the version you're running just execute "db.version()".检查 $function https://docs.mongodb.com/manual/reference/operator/aggregation/function/的文档,它说这些功能是在 4.4 版中引入的如果您对正在运行的版本有任何疑问,请执行“ db.version()”。 Mine was 4.2.17我的是 4.2.17

I hope it can help我希望它可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM