Recently I started playing around w/ Mongo for a side project and so far it has been a cake walk. While learning how to use Mongo I quickly realized that the ‘out of the box’ tool to query the database was the command line. Now this works great, but is limited. I am not able to easily save and reuse any scripts. While doing some simple research about different Admin Consoles for Mongo I noticed that I could use LINQPad to do accomplish my needs.
In order to use LINQPad there are a few steps which need to be performed, the first of which is to download and install LINQPad. Once you have this installed we can get down to the nuts and bolts.
How to reference the Mongo C# Library:
When ever I need to reference any assemblies I want to use NuGet when at all possible, fortunately I am in luck because LINQPad has native support for this.
- Create a new LinqPad C# Expression Query
- Right click the query editor and choose Query Properties
- Click the ‘Add NuGet’ button in the Query Properties dialog
- Search for the MongoCSharpProvider, click Add To Query
- Notice that the ‘Add to Query’ link changes to ‘Add Namespaces’, click this. A dialog will popup, select all the items (I am lazy and this is easier) click OK
- When you are taken back to the Query Properties window you should see the following
Now that we have Mongo Client referenced we can create the code needed to make our connection and do something useful
Connecting to Mongo in LinqPad
To do this you can add code as seen below.
Once you have added the above code you can do anything you want to your Mongo db and save the scripts for later.
Till next time,
12-18-2013 1:48 PM