Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call stored function in mongodb

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.

like image 795
Ben Rhouma Zied Avatar asked Aug 12 '13 10:08

Ben Rhouma Zied


1 Answers

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():

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

like image 185
Nanhe Kumar Avatar answered Sep 30 '22 11:09

Nanhe Kumar