Supporting custom JavaScript functions loaded to db.system.js in $function (as part of aggregation)

Hi
I’m using MongoDB 5, server is mongod.
I’m trying to do the following on one of my collections:

  1. Upload a Javascript function to db.system.js (id=‘myFunc’) - once on init. This works and function is stored.
  2. Call an “updateOne” operation with aggregation inside - this works with “simple” functions, e.g. hex_md5(). I’m able to perform an atomic update on the document i want using the $function.
  3. But when I try to call the function I saved in db.system.js, I get an error saying ‘myFunc’ is not defined…

After some searching I found this thread (https://jira.mongodb.org/browse/SERVER-49722) saying that similar behavior is not supported for $functions. Is this the case?

— Here are the queries I use: —

Add the simple myFunc (basic echo):
db.system.js.insertOne({_id: "myFunc", value : function(x) { return x; }});
response:

{
  acknowledged: true,
  insertedId: 'myFunc'
}

Add a document on the the test coll1:
db.coll1.insertOne({"metadata": "md1","data": {"field1": "foo","field2": {"sub1": "bar","sub2":"foobar"}}})

Test1 - Try to update it with the func that changes field1 to its hash:

db.coll1.updateOne(
    { "metadata":"md1" },
    [{ $set: {
        "data.field2.sub1": {
            $function: {
                body: function(name) {
                   return hex_md5(name)
                },
                args: [ "$data.field1" ],
                lang: "js"
            }
        },
    }}] 
)

works perfectly.

Test2 - now, trying to use the function I loaded to system.js:

db.coll1.updateOne(
    { "metadata":"md3" },
    [{ $set: {
        "data.field2.subf1": {
            $function: {
               body: function(x) { return myFunc(x); },
               args: [ "$data.field1" ],
               lang: "js"
            }
        },
    }}] 
)

Error response:

MongoServerError: ReferenceError: myFunc is not defined :
@:1:16
@:1:16

    at /Applications/MongoDB Compass.app/Contents/Resources/app.asar.unpacked/node_modules/@mongosh/node-runtime-worker-thread/dist/worker-runtime.js:1917:1555121
    at /Applications/MongoDB Compass.app/Contents/Resources/app.asar.unpacked/node_modules/@mongosh/node-runtime-worker-thread/dist/worker-runtime.js:1917:1394598
    at /Applications/MongoDB Compass.app/Contents/Resources/app.asar.unpacked/node_modules/@mongosh/node-runtime-worker-thread/dist/worker-runtime.js:1917:1577598
    at /Applications/MongoDB Compass.app/Contents/Resources/app.asar.unpacked/node_modules/@mongosh/node-runtime-worker-thread/dist/worker-runtime.js:1917:1576543
    at e.monitorCommands.i.cb (/Applications/MongoDB Compass.app/Contents/Resources/app.asar.unpacked/node_modules/@mongosh/node-runtime-worker-thread/dist/worker-runtime.js:1917:1387929)
    at Connection.onMessage (/Applications/MongoDB Compass.app/Contents/Resources/app.asar.unpacked/node_modules/@mongosh/node-runtime-worker-thread/dist/worker-runtime.js:1917:1385107)
    at MessageStream.<anonymous> (/Applications/MongoDB Compass.app/Contents/Resources/app.asar.unpacked/node_modules/@mongosh/node-runtime-worker-thread/dist/worker-runtime.js:1917:1382884)
    at MessageStream.emit (node:events:513:28)
    at p (/Applications/MongoDB Compass.app/Contents/Resources/app.asar.unpacked/node_modules/@mongosh/node-runtime-worker-thread/dist/worker-runtime.js:1917:1404526)
    at MessageStream._write (/Applications/MongoDB Compass.app/Contents/Resources/app.asar.unpacked/node_modules/@mongosh/node-runtime-worker-thread/dist/worker-runtime.js:1917:1403147)

How can this be solved?
Can I indeed use my JS code from system.js?
If not with $function, what other way of going around this?

Thanks a lot for your time!!