Now I need to use the Java to get some input from user, then execute one of the file or two.
after looking through the java driver, I can't find any way.
so how can I execute a javascript file by the Java Mongodb driver ?
thanks
There's no way to directly execute javascript using Java. You can spawn a mongo shell process to run the javascript using something like java.lang.Process to execute something like mongo localhost:27017/test myjsfile.js. The Process interface will allow you to get back output from the execution.
It's worth mentioning that your mongo shell scripts can also be rewritten in Java and run natively - the Java driver and the mongo shell are equally capable, though there are more convenient helper methods for administration in the mongo shell.
Even I have the same use case. I want to write and keep my database migration scripts outside Java code and run them as needed to fix existing data or schema.
After searching around, I have also picked the same approach. But something more native in Java code would be preferred :)
After searching around, I have also picked the same approach. But something more native in Java code would be preferred :)
You can use com.mongodb.DB#doEval() to execute javascript on the server if you'd like
------------------------------ --
{ name : "Justin Lee",
title : "Software Engineer",
location : "New York, NY" }
Thanks. I will look into it.
Be warned that db.eval() is slower than other options, takes a global write lock for the entirety of its execution, and cannot be used to work with sharded collections. The more native Java code option would be to rewrite the javascript tasks in the Java driver.
thanks for your reply.
댓글 없음:
댓글 쓰기