how can I execute a javascript file by the Java Mongodb driver

2,584 views
Skip to first unread message

Bo Hao

unread,
Nov 6, 2014, 2:04:16 AM11/6/14
to mongod...@googlegroups.com
I have 3 javascript files do some jobs in Mongodb. They all work good in mongoshell.
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

Will Berkeley

unread,
Nov 6, 2014, 2:44:50 AM11/6/14
to mongod...@googlegroups.com
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.

-Will

Dhananjay Makwana

unread,
Nov 6, 2014, 9:21:19 AM11/6/14
to mongod...@googlegroups.com
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 :)
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/3a146e08-4390-47c6-877d-1b38daae989c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Justin Lee

unread,
Nov 6, 2014, 9:24:50 AM11/6/14
to mongod...@googlegroups.com
You can use com.mongodb.DB#doEval() to execute javascript on the server if you'd like

--------------------------------

name     : "Justin Lee", 
  title    : "Software Engineer",
  twitter  : "@evanchooly",
  web      : [ "10gen.com", "antwerkz.com" ],
  location : "New York, NY" }

Dhananjay Makwana

unread,
Nov 6, 2014, 9:49:07 AM11/6/14
to mongod...@googlegroups.com
Thanks. I will look into it.

Will Berkeley

unread,
Nov 6, 2014, 11:28:00 AM11/6/14
to mongod...@googlegroups.com
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.

-Will

Bo Hao

unread,
Nov 6, 2014, 8:39:31 PM11/6/14
to mongod...@googlegroups.com
thanks for your reply.
Reply all
Reply to author
Forward
0 new messages