How to run js file in mongo using spring data

后端 未结 2 1286
难免孤独
难免孤独 2020-12-18 01:30

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 02:00

    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");    
    

提交回复
热议问题