For the latest stable version, please use Spring Data MongoDB 4.4.3! |
Script Operations
MongoDB 4.2 removed support for the |
MongoDB allows running 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 | Run 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 | Run the script with name 'echo' using the provided parameters. |