Script Operations

MongoDB allows executing JavaScript functions on the server by either directly sending the script or calling a stored one. ScriptOperations can be accessed through MongoTemplate and provides basic abstraction for JavaScript usage. The following example shows how to us the ScriptOperations class:

ScriptOperations scriptOps = template.scriptOps();

ExecutableMongoScript echoScript = new ExecutableMongoScript("function(x) { return x; }");
scriptOps.execute(echoScript, "directly execute script");     (1)

scriptOps.register(new NamedMongoScript("echo", echoScript)); (2)
scriptOps.call("echo", "execute script via name");            (3)
1 Execute the script directly without storing the function on server side.
2 Store the script using 'echo' as its name. The given name identifies the script and allows calling it later.
3 Execute the script with name 'echo' using the provided parameters.