首页 > 解决方案 > 如何在 3.6.4 vs 2.4 Mongo 服务器 [system.js] 中执行 javascript 函数

问题描述

目前在 2.4 MongoDB 中。如果我们在 MongoDB Server 中创建函数,它可以通过以下方式很好地测试,但在 3.6.4 中会遇到错误

蒙哥 2.4

$mongo emgda
>db.system.js.save({
...    "_id" : "getX",
...    "value" : "function(obj) { return 'Hey I got Executed.' }"
... });
>
>db.loadServerScripts()
>getX({})
Hey I got Executed.

MONGO 3.6.4

$mongo emgda
>db.system.js.save({
...    "_id" : "getX",
...    "value" : "function(obj) { return 'Hey I got Executed.' }"
... });
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
>db.loadServerScripts()
>getX({})
2019-10-03T13:35:22.583+0530 E QUERY    [thread1] TypeError: getX is not a function :
@(shell):1:1

谁能指导我正确执行Mongo 3.6.4中的功能。

注意:它可以执行为db.eval(getX, {}),但不推荐使用。我需要正确的执行方式。

标签: mongodbmongodb-querymongodb-3.6.4

解决方案


推荐阅读