0

Currently in 2.4 MongoDB. If we create functions in MongoDB Server it can be tested well in the below way but facing error with 3.6.4

MONGO 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

Can any one please guide me through the correct way of executing the function in Mongo 3.6.4.

NOTE: It can be executed as db.eval(getX, {}), but this is deprecated. I need the correct way of execution.

Ankur Soni
  • 5,725
  • 5
  • 50
  • 81
  • 2
    Umm that's a typo. You do `"value" : function(obj) { return 'Hey I got Executed.' }` and not `"value" : "function(obj) { return 'Hey I got Executed.' }"` as you have done. It's actually a `function()` as the argument to value and not a string. Shown [clearly in the documentation as well](https://docs.mongodb.com/manual/tutorial/store-javascript-function-on-server/). *IF* there was any previous behavior that accepted a string ( and I'm pretty sure there was not ) then that would have been a bug. – Neil Lunn Oct 03 '19 at 08:44
  • 1
    It probably should be noted that "server stored" scripts have very little practical usage. Any possible usage you are probably thinking of would likely not be the correct pattern, and there would be indeed much better modern alternatives. – Neil Lunn Oct 03 '19 at 08:46

0 Answers0