2

Of curiosity, I would like to know, how anyone would implement a custom mongo shell in .Net. allowing the user to do the exact same things as you can in the mongo shell, but implemented in .Net with the option of enhancing the user experience.

I have found this thread which tells me, that I cannot use the official 10gen driver to archieve this: Using MongoDB shell commands on MongoDB 10Gen's driver

What is the appropriate way of doing this? I guess the ultimate custom solution would be to convert the source code for the mongo shell into .Net by hand?

Any suggestions are very much appreciated as I have been googling for answers in quite some time. Thankyou in advance!

5
  • yep, you need to translate shell language into driver language. Commented Feb 14, 2012 at 17:58
  • But I would like to make a "driver" that accepts native javascript. A suggestion is made in this post for using the mongo.exe file as the executor of the commands: stackoverflow.com/questions/7002975/… Commented Feb 14, 2012 at 18:07
  • Indeed, all kinds of crazy suggestions can be made. :) Commented Feb 14, 2012 at 18:13
  • But my intuition says, that it can be done as the mongo shell is a client admin tool and connects with the server using TCP. I just can't find out where to start :) Commented Feb 14, 2012 at 18:23
  • Start with looking at mongo utility source code. Commented Feb 14, 2012 at 18:24

2 Answers 2

0

The best answer to your request to "do the exact same things as you can in the mongo shell" depends on what you are thinking of as "exact same things".

At the lowest level, the mongo shell just sends messages using the documented "wire protocol", which is a bit of wrapping around BSON data, the documented binary version/extension of JSON. So, at a low level (by sending messages over TCP/IP) you can do anything you want.

At a slightly higher level, the mongo shell includes a JavaScript "engine", which in currently released versions is SpiderMonkey version 1.7 from the Mozilla Foundation, also open source. A future version will use Google's V8 JavaScript engine, again open source and available for download. The shell provides some native code functions for those engines to use: see engine_spidermonkey.cpp and engine_v8.cpp in the MongoDB source code.

At a still higher level, the shell includes some "helper functions" written in JavaScript that simplify typing some commands. You can see these functions from the shell's prompt by typing the name of a function call and leaving off the parentheses: typing "sleep(500)" will sleep for 500 milliseconds, typing "sleep" will show that "sleep" calls "nativeHelper.apply(sleep_, arguments)". All of these helper functions are in .js files in the "shell" directory in the MongoDB source code, which you can download.

Beyond that, there is a command line loop that provides command line editing and recall, code for processing arguments on the invoking command line and code to read and write BSON-formatted data for communication with the server, very similar to what the C# driver provides.

So, you can use any of this code that is helpful to you or write similar or different code yourself. There isn't really any magic hidden away, it's all open source.

If you know what you want to accomplish and get stuck on the details of how to do something, ask your specific question and hopefully you'll get less general answers than this one.

Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou very much for your answer! What I would like to do is reuse the shell functionality at the loop-level, which you mentioned. I made it work parsing commands to the shell.exe app and get the result inside my custom C# app. I have also previously used the Javascript .Net project and started creating a javascript api from scatch converting js model and js functions into calls to the official .net driver. But this seemed like a lot of work and I would just be reinventing everything that already existed in the shell.
0

So first question, is there a particular reason that you want to do this?

First off, you can access the shell via mongo.exe from either cmd or PowerShell. Making a console app that hosts either of these is pretty trivial.

On top of that, the MongoDB driver already supports basically all of the shell functionality. In fact, all of the drivers are just translations of the JS syntax into something more reasonable for the given language. So you don't really need such a "shell" to use MongoDB from code.

That stated, there are several ways that you could write your own shell in .NET, but I'm not clear about why you want to do this. Are you just trying out a "for fun" project? Are you trying to provide query support to users? Do you have some app that needs to "drop down" to a connected shell?

Is there something specific you are trying to accomplish?

I can update this answer with more details if you can specify what you want this "shell" to accomplish.


I wan't to make provide query support for the user and at the same time present the result in a nice way.

If all you want to write it a query engine, this should be pretty easy to write in C#. It's just some simple parsing. You can take the JSON portion of the query and parse it into a BSonDocument and then pass that to the query in the MongoDB driver. The return value will be another BSonDocument that you can format as you like.

Of course, if you want to provide a query tool, you can do much better than just the shell. Take a look at the MongoVue or MongoExplorer which are both written in .NET. Those are much better versions of "ad-hoc" queries.

3 Comments

Thankyou for your answer! I wan't to make provide query support for the user and at the same time present the result in a nice way. What are your suggestions for doing such a thing? I can see, there is a MongoDatabase.Eval method on the C# driver, which evaluates db.MyCol.findOne() correctly.
I realised, this Eval method works fine with "db.MyCol.find().count()" but does not do the job when it come to "var v = function(){...}; db.MyCol.foreach(v);" so I guess this won't be the perfectly working solution. Would it be possible to make a console app that hosts mongo.exe, parses string commands o the shell program and receives the result string?
I found this post: stackoverflow.com/questions/9003778/… ...which contains a solution for calling mongo.exe with an eval parameter, but there is a problem: Executing db.MyCol.findOne() returns an object. db.MyCol.find(); gives the result {DBQuery: FooDb.MegaCol -> undefined}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.