MongoDB查询执行存储功能?
可以使用称为system.js的系统集合来保存JavaScript函数以备重用。要存储函数,请使用db.collection.save(),
让我们首先创建一个函数。以下是查询-
> db.system.js.save({
... _id: "displayMessage",
... value: function (data) {
... return 'The Name is: ' + data;
... }
... })这将产生以下输出-
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : "displayMessage"
})以下是执行存储功能的查询-
> db.eval("displayMessage('John')")
WARNING: db.eval is deprecated这将产生以下输出-
The Name is: John