[SERVER-49722] Allow use db.system.js functions in $function Created: 19/Jul/20  Updated: 22/Sep/20  Resolved: 22/Sep/20

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Question Priority: Minor - P4
Reporter: Roman Cherepanov Assignee: Katya Kamenieva
Resolution: Won't Fix Votes: 1
Labels: qopt-team
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Sprint: Query 2020-10-05
Participants:

 Description   

Are there any plans to allow the using db.system.js stored functions in $function? Like $where or Map-Reduce. I haven't found this feature in the 4.4.0-rc13 version

 



 Comments   
Comment by Katya Kamenieva [ 22/Sep/20 ]

hi roman.cherepanov@gmail.com, thank you for the detailed description of your request.

We are not planning on expanding the use of system.js for storing user-defined functions, as this approach doesn't work well in many cases. For example, in sharded clusters, javascript functions will have to be created on every shard.

We will be looking for better alternatives to provide storing of the functions in the future, but currently this work is not scheduled.

Comment by Dmitry Agranat [ 13/Sep/20 ]

Thanks for the steps roman.cherepanov@gmail.com and apologies for a delayed response.

You are correct that when using mapReduce the use of function works but does not work when using this directly in $function. This is also in-line with the goals of this new functionality where we added support to allow loading system.js functions for $where and MapReduce, but do not support this behavior for UDFs.

We're assigning this ticket to the appropriate team to be evaluated against our currently planned work.

Thanks,
Dima

Comment by Roman Cherepanov [ 19/Aug/20 ]

Here are all the steps where the error is reproduced:

> use Test
switched to db Test
> db.createCollection("test_f")
{ "ok" : 1 }
> db.test_f.insert( { name: "John" } )
WriteResult({ "nInserted" : 1 })
> db.system.js.insertOne(
...    {
...      _id: "echoFunction",
...      value : function(x) { return x; }
...    }
... );
{ "acknowledged" : true, "insertedId" : "echoFunction" }
> var map = function() { emit("echoName", echoFunction(this.name)); };
> var reduce = function(key, values) { return values[0]; };
> db.test_f.mapReduce(map, reduce,{out : {inline : 1}});        "results" : [
                {
                        "_id" : "echoName",
                        "value" : "John"
                }
        ],
        "timeMillis" : 25,
        "counts" : {
                "input" : 1,
                "emit" : 1,
                "reduce" : 0,
                "output" : 1
        },
        "ok" : 1
}> db.test_f.aggregate([
...   { $project: {
...      "echoName": { $function:
...                {
...                   body: function(name) {
...                      return echoFunction(name)
...                   },
...                   args: [ "$name" ],
...                   lang: "js"
...                }
...             },
...   }}])
uncaught exception: Error: command failed: {
        "ok" : 0,
        "errmsg" : "ReferenceError: echoFunction is not defined :\n@:2:22\n",
        "code" : 139,
        "codeName" : "JSInterpreterFailure"
} : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:18:14
_assertCommandWorked@src/mongo/shell/assert.js:618:17
assert.commandWorked@src/mongo/shell/assert.js:708:16
DB.prototype._runAggregate@src/mongo/shell/db.js:266:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1046:12
@(shell):1:1
>

Comment by Dmitry Agranat [ 10/Aug/20 ]

Hi roman.cherepanov@gmail.com, could you please provide all the steps you've used during this test so that we'll be able to reproduce this?

Comment by Roman Cherepanov [ 03/Aug/20 ]

Hi Dmitry,

I've stored my custom function using [Store a JavaScript Function on the Server|https://docs.mongodb.com/manual/tutorial/store-javascript-function-on-server/index.html] manual.

But when I call this function in $function statement I've got ReferenceError. However, it works in mapReduce.

Tested in 4.4.0

Comment by Dmitry Agranat [ 02/Aug/20 ]

Hi roman.cherepanov@gmail.com,

Here is the blog post that was published about the release and its key highlights. Additionally, here is the announcement that went out to the community.

Regarding your request, does Store a JavaScript Function on the Server address your question?

Thanks,
Dima

Generated at Thu Feb 08 05:20:38 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.