mongodb - how to run js file in mongo using springs -
i use following code run mongo script in js through spring data
scriptoperations scriptops = mongooperation.scriptops(); // execute script directly executablemongoscript echoscript = new executablemongoscript("function(x) { return x; }"); object ob=scriptops.execute(echoscript, "directly execute script"); system.out.println(ob); // register script , call later scriptops.register(new namedmongoscript("runjs", "d:\\gstr3\\gstr3b.1.a.js")); scriptops.call("runjs", "execute script via name");
the "echoscript" run "runjs" gives error:
caused by: com.mongodb.commandfailureexception: { "serverused" : "/10.144.113.138:27017" , "ok" : 0.0 , "errmsg" : "referenceerror: runjs not defined :\n_funcs1@:1:24\n" , "code" : 139 , "codename" : "jsinterpreterfailure"} @ com.mongodb.commandresult.getexception(commandresult.java:71) @ com.mongodb.commandresult.throwonerror(commandresult.java:110) @ com.mongodb.db.eval(db.java:358) @ org.springframework.data.mongodb.core.defaultscriptoperations$2.doindb(defaultscriptoperations.java:119) @ org.springframework.data.mongodb.core.mongotemplate.execute(mongotemplate.java:446) ... 2 more
but if run script node.js, result:
d:\gstr3>node gstr3b.1.a.js { gstin: '27aixpt3280a1z2', t_ival: 100003.78, t_iamt: 0, t_camt: 173520.1, t_samt: 173527.69999999998, t_itm_count: 33 } d:\gstr3>
scriptops.register takes executablemongoscript argument, syntax: scriptops.register(executablemongoscript arg0) , namedmongoscript takes 2 arguments, syntax: namedmongoscript(string name, executablemongoscript script), never takes path of javascript.
so, can run .js script java processor , wait completion , output input stream.
try{ string nodejspath = "d:\\gstr3\\gstr3b.1.a.js"; string cmd = "node " + nodejspath; runtime run = runtime.getruntime(); process pr = run.exec(cmd); pr.waitfor(); } catch (interruptedexception ex) { ex.printstacktrace(); }
Comments
Post a Comment