Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 other way in Java. I've tried:

BasicDBObject obj = new BasicDBObject();
obj.append( "$load" , "/path/file.js" );
CommandResult t=mongoTemplate.executeCommand(obj);

and:

obj.append( "$eval" , "load("/path/file.js")" );

but it doesn't work.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
307 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...