MongoDB::execute
Runs JavaScript code on the database server.
&reftitle.description;
public arrayMongoDB::execute
mixedcode
arrayargsarray()
The Mongo database server runs a JavaScript engine. This method allows you
to run arbitary JavaScript on the database. This can be useful if you want
touch a number of collections lightly, or process some results on the
database side to reduce the amount that has to be sent to the client.
Running JavaScript in the database takes a write lock, meaning it blocks
other operations. Make sure you consider this before running a long script.
This is a wrapper for a database command. This method is basically:
command(array('$eval' => $code, args => $args));
}
?>
]]>
&reftitle.parameters;
code
MongoCode or string to execute.
args
Arguments to be passed to code.
&reftitle.returnvalues;
Returns the result of the evaluation.
&reftitle.examples;
Simple MongoDB::execute example
execute("function() { return 'Hello, world!'; }");
echo $response['retval'];
?>
]]>
&example.outputs.similar;
Hello, world!
Parameter MongoDB::execute example
The optional array of parameters will be passed to the JavaScript function.
execute("function(greeting, name) { return greeting+', '+name+'!'; }", array("Good bye", "Joe"));
echo $response['retval'];
?>
]]>
&example.outputs.similar;
Good bye, Joe!
Scope example
If a MongoCode object is used instead of a string for
the first parameter, a scope can be passed in which the JavaScript will be
executed.
"Fred");
$code = new MongoCode($func, $scope);
$response = $db->execute($code, array("Goodbye", "Joe"));
echo $response['retval'];
?>
]]>
&example.outputs.similar;
Goodbye, Joe, says Fred