I'm getting some difficulties to call a stored function within mongdb. I'm a little bit newbie with mongo.
[EDIT] : my function stored in 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.
There is a special system collection named
that can store JavaScript functions for reuse.
To store a function, you can use the
, as in the following examples:
The _id field holds the name of the function and is unique per database.
The value field holds the function definition.
In the mongo shell, you can use
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:Source: MONGODB MANUAL
I think you need to use $where for it to work! something like this:
for more information read this: https://docs.mongodb.com/manual/reference/operator/query/where/
Consider the following example from the mongo shell that first saves a function named
echoFunction
to thesystem.js
collection and calls the function usingdb.eval()
:echoFunction(...)
is available ineval
/$where
/mapReduce
etc. more information is available at http://docs.mongodb.org/manual/tutorial/store-javascript-function-on-serverYou can call your function like this
if my function is stored in db using command:
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