Java Driver runCommand as in shell

1,119 views
Skip to first unread message

MongoDiagrams

unread,
Nov 29, 2015, 6:24:02 AM11/29/15
to mongodb-user
Hi,
I am building a tool similar with RoboMongo in Java.
I am looking a way to execute text queries and map-reduce jobs using the Java driver.
Is the MongoDatabase#runCommand() the proper way ? 
This works fine with a findOne() query which returns the result document, but find() doesn't work or at least I don't know how to get a cursor or similar.
Can you please advice ?

Thank you,
Dragos P.

Stephen Steneker

unread,
Nov 30, 2015, 1:06:02 AM11/30/15
to mongodb-user

On Sunday, 29 November 2015 22:24:02 UTC+11, Dragos wrote:

I am building a tool similar with RoboMongo in Java.
I am looking a way to execute text queries and map-reduce jobs using the Java driver.
Is the MongoDatabase#runCommand() the proper way ?
This works fine with a findOne() query which returns the result document, but find() doesn’t work or at least I don’t know how to get a cursor or similar.

Hi Dragos,

The runCommand() helper is used to run a database command on the server, which typically is at a much lower level than a text string provided by the end user.

The MongoDB Java driver has a MapReduceCommand class which helps construct a command for Map/Reduce. If you want to return a find() query as a cursor, you should use DBCollection#find().

If your goal is to transform user-provided text queries into BSON queries, you will need to parse the text rather than sending it directly to the server.

You might want to look into Jongo (http://jongo.org), which aims to provide a query interface in Java which is very similar to the mongo shell interface.

Regards,
Stephen

Dragos

unread,
Nov 30, 2015, 3:46:30 AM11/30/15
to mongodb-user
My target is to build a tool similar with RoboMongo in Java, plus other functionality.
I understand I can use Jongo, but the language is still different. I want to let the server to complie and execute the code, is not my job to do this.
If the C# driver can do this, I expect the Java driver can do this as well. RoboMongo is sending the text command to the server, I want to do the same from Java.
Could you please help me ?
Thank you,
Dragos

Ross Lawley

unread,
Nov 30, 2015, 6:35:26 AM11/30/15
to mongod...@googlegroups.com
Hi,

It looks like you are using the 3.x Mongo Java Driver - so MongoDatabase#runCommand() will work.  You can parse the user json input via Document.parse(json), without needing any other dependencies.  The downside to using runCommand is you will manually have to create the commands which may change depending on the MongoDB version the users is connecting to.

It would be easier to use the API directly.  For example passing the Strings for the Map and Reduce functions and calling: MapReduceIterable (the iterable() method will return a cursor).  Also using the find method to query the database would be much simpler.  This way the Java driver does the heavy lifting and version checking for the commands.

I hope that helps,

Ross

--
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/c30da100-8457-48f9-bbb1-281412b38795%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--


{ name     : "Ross Lawley",
  title    : "Senior Software Engineer",
  location : "London, UK",
  twitter  : ["@RossC0", "@MongoDB"],
  facebook :"MongoDB"}

Stephen Steneker

unread,
Nov 30, 2015, 7:03:09 AM11/30/15
to mongodb-user
On Monday, 30 November 2015 19:46:30 UTC+11, Dragos wrote:
My target is to build a tool similar with RoboMongo in Java, plus other functionality.
I understand I can use Jongo, but the language is still different. I want to let the server to complie and execute the code, is not my job to do this.
If the C# driver can do this, I expect the Java driver can do this as well. RoboMongo is sending the text command to the server, I want to do the same from Java.

Hi Dragos,

Are you trying to implement the shell interface as embedded in Robomongo, or a subset like queries & aggregates? What would your "text commands" look like - "db.collection.find({...})", or just the query text?

Robomongo uses the codebase of the mongo shell, which has a C++ backend with JavaScript frontend helper functions (and an embedded client-side JavaScript engine). JavaScript queries are parsed in the client application and converted to BSON messages for the server, as with any MongoDB driver. The client-side JavaScript engine maintains some context for user-defined functions and variables. Robomongo also uses a few other editing components which do not strictly match the mongo shell behaviour (QScintilla and QJSON).

The mongo shell (as at MongoDB 3.2) is not cleanly separated from the server codebase, so linking directly to this code isn't a very maintainable approach (as compared to using a supported driver). You can have a look at the shell code on GitHub: https://github.com/mongodb/mongo/tree/master/src/mongo/shell.

If you want to avoid implementing similar parsing logic to the mongo shell (but want to implement a similar interface) one approach to consider would be piping output to a mongo shell process. As a bonus, this approach would allow your application to support multiple versions of the mongo shell instead of embedding a specific version. Some admin tools (eg. MongoChef) use both a supported driver and this approach of wrapping the official shell binary.

Regards,
Stephen

Dragos

unread,
Dec 1, 2015, 1:42:05 AM12/1/15
to mongodb-user
Great, thank you Stephen,
 Thank you very much for ideas. I will check probably the solution with embedded JavaScript.
-r
Dragos
Reply all
Reply to author
Forward
0 new messages