6

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.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Ben Rhouma Zied
  • 2,473
  • 3
  • 19
  • 29
  • 1
    Can you please explain what you are trying to accomplish? – Derick Aug 12 '13 at 11:04
  • 2
    Strange how everyone answers in console yet the question was and is tagged with java `:\` – Sammaye Aug 12 '13 at 11:37
  • Don't use javascript functions for this kind of features on the server as they can 1.only run on the primary and 2.need to take a write lock on the db during the whole execution causing a drop in throughput. – christkv Aug 13 '13 at 11:33

5 Answers5

16

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

Nanhe Kumar
  • 15,498
  • 5
  • 79
  • 71
15

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

db.loadServerScripts();

mySampleFunction(3, 5);
byteC0de
  • 5,153
  • 5
  • 33
  • 66
8

There is a special system collection named

system.js

that can store JavaScript functions for reuse.

To store a function, you can use the

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 _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

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:

db.loadServerScripts();

echoFunction(3);

myAddFunction(3, 5);

Source: MONGODB MANUAL

KhogaEslam
  • 2,528
  • 1
  • 20
  • 21
5

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.system.js.save(
 {
  _id : "myFunction" ,
  value : function (x){ return x + 1; }
  });
Kiwi Rupela
  • 2,238
  • 5
  • 24
  • 44
-1

I think you need to use $where for it to work! something like this:

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/

saeedeh
  • 89
  • 1
  • 13