How to run js file in mongo using spring data

后端 未结 2 1879
夕颜
夕颜 2020-12-18 01:40

In mongo shell js file can be run using load command:

load(\"path/to/file/file.js\")

How to do this using spring-data? Or any

2条回答
  •  [愿得一人]
    2020-12-18 01:48

    Here's the relevant section of the reference docs on how to work with scripts in Spring Data MongoDB.

    ScriptOperations scriptOps = template.scriptOps();
    
    // Execute script directly
    ExecutableMongoScript echoScript = new ExecutableMongoScript("function(x) { return x; }");
    scriptOps.execute(echoScript, "directly execute script");     
    
    // Register script and call it later
    scriptOps.register(new NamedMongoScript("echo", echoScript)); 
    scriptOps.call("echo", "execute script via name");    
    

提交回复
热议问题