在 mongodb 中调用存储的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18185192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call stored function in mongodb
提问by Ben Rhouma Zied
I'm getting some difficulties to call a stored function within mongdb. I'm a little bit newbie with mongo.
我在 mongdb 中调用存储函数时遇到了一些困难。我是 mongo 的新手。
[EDIT] : my function stored in mongodb
[编辑]:我的函数存储在 mongodb 中
function() {
var cndTime = new Date().getTime() - (3600*1000*24*2); // condition
db.urls.find(needParse: false).forEach(function(item){
if(item.date < cndTime) // check
db.urls.update({_id: item._id}, {$set: { needParse: true }}); // update field
});
}
All I'm asking is how to invoke this function using reactivemongo or native API.
我要问的是如何使用reactivemongo 或本机API 调用此函数。
回答by Nanhe Kumar
Consider the following example from the mongo shell that first saves a function named echoFunction
to the system.js
collection and calls the function using db.eval()
:
考虑以下来自 mongo shell 的示例,该示例首先将命名的函数保存echoFunction
到system.js
集合中,并使用 调用该函数db.eval()
:
db.system.js.save({
_id: "echoFunction",
value: function (x) {
return 'echo: ' + x;
}
})
db.eval("echoFunction('test')") // -> "echo: test"
echoFunction(...)
is available in eval
/$where
/mapReduce
etc. more information is available at http://docs.mongodb.org/manual/tutorial/store-javascript-function-on-server
echoFunction(...)
是,可以在eval
/ $where
/mapReduce
等的更多信息可在http://docs.mongodb.org/manual/tutorial/store-javascript-function-on-server
回答by byteC0de
In the mongo shell, you can use db.loadServerScripts() to load all the scripts saved in the system.js collection for the current database. Once loaded, you can invoke the functions directly in the shell, as in the following example
在 mongo shell 中,您可以使用 db.loadServerScripts() 为当前数据库加载保存在 system.js 集合中的所有脚本。加载后,您可以直接在 shell 中调用函数,如下例所示
db.loadServerScripts();
mySampleFunction(3, 5);
回答by KhogaEslam
There is a special system collection named
有一个名为的特殊系统集合
system.js
系统.js
that can store JavaScript functions for reuse.
可以存储 JavaScript 函数以供重用。
To store a function, you can use the
要存储函数,您可以使用
db.collection.save()
db.collection.save()
, as in the following examples:
,如下例所示:
db.system.js.save(
{
_id: "echoFunction",
value : function(x) { return x; }
}
);
db.system.js.save(
{
_id : "myAddFunction" ,
value : function (x, y){ return x + y; }
}
);
The _idfield holds the name of the function and is unique per database.
该_id字段保存功能的名称,每个数据库都有唯一的。
The valuefield holds the function definition.
该值字段保存函数定义。
In the mongoshell, you can use
在mongoshell 中,您可以使用
db.loadServerScripts()
db.loadServerScripts()
to load all the scripts saved in the system.js
collection for the current database. Once loaded, you can invoke the functions directly in the shell, as in the following example:
加载保存在system.js
当前数据库集合中的所有脚本。加载后,您可以直接在 shell 中调用函数,如下例所示:
db.loadServerScripts();
echoFunction(3);
myAddFunction(3, 5);
Source: MONGODB MANUAL
来源:MongoDB 手册
回答by Kiwi Rupela
You can call your function like this
你可以这样调用你的函数
db.loadServerScripts();
db.data.insert({
_id: myFunction(5),
name: "TestStudent"
});
if my function is stored in db using command:
如果我的函数使用命令存储在 db 中:
db.system.js.save(
{
_id : "myFunction" ,
value : function (x){ return x + 1; }
});
回答by saeedeh
I think you need to use $where for it to work! something like this:
我认为您需要使用 $where 才能使其工作!像这样:
db.urls.find( { needParse: false, $where: function(item){
if(item.date < cndTime) // check
db.urls.update({_id: item._id}, {$set: { needParse: true }});
for more information read this: https://docs.mongodb.com/manual/reference/operator/query/where/
有关更多信息,请阅读:https: //docs.mongodb.com/manual/reference/operator/query/where/