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?