Nó được lưu trong bộ sưu tập system.js đặc biệt. Đối với điều này, hãy sử dụng db.system.js.save (). Sau đây là cú pháp -
db.system.js.save({
_id: "anyFunctionName",
value: function (returnValue) {
return ‘yourMessage ' + returnValue;
}
}) Hãy để chúng tôi thực hiện cú pháp trên. Sau đây là truy vấn -
> db.system.js.save({
... _id: "returnValue",
... value: function (data) {
... return 'The value==== ' + data;
... }
... })
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) Sau đây là truy vấn để gọi hàm trên để in dữ liệu thực tế -
> db.eval("returnValue(20)")
WARNING: db.eval is deprecated Điều này sẽ tạo ra kết quả sau -
The value==== 20