mongo jsinterpreter syntaxerror:意外令牌:关键字' function'

发布于 2025-02-07 19:44:06 字数 1248 浏览 1 评论 0原文

我正在使用mongodb 4.4.8,根据$ function是4.4中的新事物,但是运行以下代码,服务器响应:

在我的配置文件中,security.javascriptenabled,所以我认为应该它默认为true

命令失败,错误139(JSInterPreterFailure):'SyntaxError:意外令牌:preateword'function'''{}}。完整的响应是{“ ok”:0.0,“ errmsg”:“ syntaxerror:意外令牌:关键字'函数'“”,“ code”:139,“ codeName”:“ jsinterpreterfailure”}

    db.my_collection.aggregate(
    [
        {$addFields:{
            query: {
                $function:{
                    body: function(ss){
                        arr = ss.split('|');
                        result = {};
                        arr.forEach(element => {
                            kv = element.split(':');
                            result[kv[0]] = kv[1];
                        });
                        return result['q']},
                    args: ['my_field'],
                    lang:'js'
                }
            }
            }}
    ]);

函数可以在我的计算机上成功执行,但是Mongo似乎无法解析关键字函数,发生了什么?

I am using MongoDB 4.4.8, according to mongo reference, $function is new in 4.4, but running the following code, the server responded:

In my config file, security.javascriptEnabled is not set, so I think it should be default to true

Command failed with error 139 (JSInterpreterFailure): 'SyntaxError: unexpected token: keyword 'function'' on server {}. The full response is {"ok": 0.0, "errmsg": "SyntaxError: unexpected token: keyword 'function'", "code": 139, "codeName": "JSInterpreterFailure"}

    db.my_collection.aggregate(
    [
        {$addFields:{
            query: {
                $function:{
                    body: function(ss){
                        arr = ss.split('|');
                        result = {};
                        arr.forEach(element => {
                            kv = element.split(':');
                            result[kv[0]] = kv[1];
                        });
                        return result['q']},
                    args: ['my_field'],
                    lang:'js'
                }
            }
            }}
    ]);

The function can be executed successfully on my computer, but mongo seemed not be able to parse the keyword function, what happened?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瓜 2025-02-14 19:44:06

我遇到了同样的问题,但是我直接在Mongo Shell中尝试了,并且它起作用了,这可能是Datagrip的问题,而不是Mongo令人惊讶。至于为什么?我不知道。

因此,尝试在控制台中尝试相同的聚合

I had the same problem but I tried in the mongo shell directly and it worked, might be a problem with DataGrip rather than with mongo surprisingly. As to why? I don't know.

So try the same aggregation in the console instead

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文