它保存在特殊的system.js集合中。为此,请使用db.system.js.save()。以下是语法-
db.system.js.save({
_id: "anyFunctionName",
value: function (returnValue) {
return ‘yourMessage ' + returnValue;
}
})
让我们实现以上语法。以下是查询-
> db.system.js.save({
... _id: "returnValue",
... value: function (data) {
... return 'The value==== ' + data;
... }
... })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
以下是调用上述函数以打印实际数据的查询-
> db.eval("returnValue(20)")
WARNING: db.eval is deprecated
这将产生以下输出-
The value==== 20