0

I created a Stored JS function which supposedly completes a very tedious job, faster and efficiently. Now after a investing long working hours I came up with completing this functionality.

So my function is suppose myFunc();

db.system.js.save(
{
 _id : "myFunc" ,
 value : function (param1,param2,param3....){ ... }});

Once I was through with completing this function, and testing it with Mongo Shell as

db.eval("myFunc('a','b'...)");

and

db.loadServerScripts();
myFunc('a','b',..);

I was satisfied it will work, Now the problem begins, How to achieve this with PHP?

$cmd = 'db.eval("myFunc(\'a\',\'b\'...);");';
$data = $mongo_db->execute($cmd);
var_dump($data);

$data comes up with nothing..!!

array(2) { ["retval"]=> NULL ["ok"]=> float(1) } 

Somebody tell me please what the heck am I doing wrong..!!

2
  • Please provide a full example that can be copied and pasted. Having ... is not very useful. Commented Nov 27, 2013 at 12:25
  • Weel the code is about 50 loc. And I it makes 2 queries in it. But issue is resolved by a workaround. :) Still Thanks..!! Commented Nov 29, 2013 at 10:04

2 Answers 2

2

You have a similar question at: Calling a stored procedure via PHP in MongoDB

Anyway as mongodb docs says:

Note We do not recommend using server-side stored functions if possible.

Is a better practice execute directly the JS with MongoDB::execute instead of store it in the db.system.js and after call it:

$func = 
    "function(greeting, name) { ".
        "return greeting+', '+name+', says '+greeter;".
    "}";
$scope = array("greeter" => "Fred");

$code = new MongoCode($func, $scope);

$response = $db->execute($code, array("Goodbye", "Joe"));
echo $response['retval'];

From: http://php.net/manual/en/mongodb.execute.php

With the MongoCode object you can use scoping and create PHP objects with you JS, and you can have this JS on your PHP project at the vcs, all together.

Sign up to request clarification or add additional context in comments.

3 Comments

?¿ Then how you make MapReduce without JS? You must have caution but about the load on the server, but you MUST use JS if you work with MongoDB.
Map reduce is not JS in the sense of this, MR runs completely differently, for one it is a database command that accepts JS callbacks
So, I understand it is not advised to use JS stored function, but I have a strong server to support my needs, and moreover I am trying to avoid calculations at my requesting server. And also using no lock for eval.
0

The Simple workaround I found:

var out = db.eval("abc(param1,param2..)"); return(JSON.stringify(out));

I know its not at all advisable way to achieve this, but till the time I find a better workaround( as Stored JS is not an advisable method ) its the only way I have. Will post in case of any failure.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.