Executing Javascript code for embedded MongoDB Shell through Java

930 views
Skip to first unread message

Jesús Joaquín Martínez Molina

unread,
Mar 14, 2014, 4:32:53 AM3/14/14
to mongod...@googlegroups.com
Dear all. This is my first question in this group :)

Is it possible to execute Javascript code through Java with MongoBD Java Driver??

I see this question in Stackoverflow http://stackoverflow.com/questions/4860728/executing-mongo-like-query-jsonthrough-java , but I don't know if currently, Java driver allows to develop a query of this type.

For example, I want to execute through my Java application queries like: 
 
  ”db.users.find( { ‘name’ : ‘John’} )”

Thank in advance.

Best regards, Jesús.

Gaurang Patel

unread,
Mar 17, 2014, 8:30:16 AM3/17/14
to mongod...@googlegroups.com
Hi,

I have also the same problem and after searching many articles and threads I feel it is not possible to directly execute mongo shell script from Java driver as in the case of jdbc execute for relational database.

Thanks

Stephen Steneker

unread,
Mar 17, 2014, 9:10:42 AM3/17/14
to mongod...@googlegroups.com
Hi Gaurang, Jesús,

The `mongo` shell provides a JavaScript interface to MongoDB, but the underlying communication to the MongoDB server for all drivers uses the MongoDB Wire Protocol: http://docs.mongodb.org/meta-driver/latest/legacy/mongodb-wire-protocol/. This means there isn't an equivalent of a generic "execute" for a string on the server; you have to use the interface provided by your driver.

It sounds like you are both looking for a Java interface which is higher level than the standard MongoDB Java driver .. do you have more details on your use cases? For example, if you are intending to pass through user-provided input directly to MongoDB you should definitely reconsider (best practice would be to build up the query programmatically so you can sanity check the values provided and perhaps abstract some of the implementation details).

You might want to look into the Jongo community-supported driver, whose goal is to "Query in Java as in Mongo Shell": http://jongo.org/.

Regards,
Stephen 

Jesús Joaquín Martínez Molina

unread,
Mar 17, 2014, 9:12:16 AM3/17/14
to mongod...@googlegroups.com
Hi, 

I'm testing to convert a String to DBObject with JSON.parse(String) and then I can execute commands like String query = "{'name': 'myName'} "; and then writting :
DBCursor cursor = coll.find((DBObject)JSON.parse(query));

This allows to me to execute some "filters". But, of course, I have yet the problem that I can't execute directly like in a relational db :(

Best regards, Jesús

Jesús Joaquín Martínez Molina

unread,
Mar 17, 2014, 11:05:10 AM3/17/14
to mongod...@googlegroups.com
Hi Stephen,

my idea is developping  a Java application that allows to write a query and execute on line, without turn off my app. For this, I have a method that receives a String parameter, that includes all the features about the query. 

For this, now I'm parsing this string to a DBObject (with JSON.Parse(String)) and then I can to launch my querie.The idea is that the string can be the JavaScript query of MongoDB shell. But at this moment, I can run my queries :)

Thanks!
Reply all
Reply to author
Forward
0 new messages