MongoDB query to execute stored function?

MongoDBDatabaseBig Data Analytics

JavaScript function can be saved for reuse using a system collection called system.js. To store a function, use the db.collection.save(),

Let us first create a function. Following is the query −

> db.system.js.save({
...    _id: "displayMessage",
...    value: function (data) {
...       return 'The Name is: ' + data;
...    }
... })

This will produce the following output −

WriteResult({
   "nMatched" : 0,
   "nUpserted" : 1,
   "nModified" : 0,
   "_id" : "displayMessage"
})

Following is the query to execute stored function −

> db.eval("displayMessage('John')")
WARNING: db.eval is deprecated

This will produce the following output −

The Name is: John
raja
Published on 02-Apr-2020 13:14:18
Advertisements