Basics of MongoDB with .Net

Basics of MongoDB with .Net

Watch the “Introduction to MongoDB”

http://www.youtube.com/watch?v=iRDOGkFKBRw&feature=player_embedded#at=95

Download MongoDB for Windows 64bit:

http://www.mongodb.org/downloads

Unzip to C:Mongo

Follow these instructions for installation:

http://www.mongodb.org/display/DOCS/Quickstart+Windows

Run these two applications (can be setup as a Windows Service)

  1. Mongod.exe first (which will stay open)
  2. Mongo.exe second, which will run and connect to Mongod.exe

 

Now connect to your Test database:

  1. Open a command prompt.
  2. Browse to c:Mongobin
  3. Type: mongod
  4. Make sure you leave this running (this is your database server)

 

Alternatively you can create a shortcute with:

Target: C:WindowsSystem32cmd.exe /k mongod

Start in: C:Mongobin

Create your first document:

  1. Open a new command prompt.
  2. Browse to c:Mongobin
  3. Type: mongo (this connects you to the database server running locally)
  4. Type: db and hit enter to see what database you’re connected to.
  5. Type:db.foo.insert({a:1}) (inserts a document)
  6. Type: db.foo.find() (does a search on the document database)

Great!

.Net Setup for MongoDB

Download drivers for MongoDB C# and .Net.

MongoDB C# driver MSI:

https://github.com/mongodb/mongo-csharp-driver/downloads

Download and run.

Community supported driver:

https://github.com/samus/mongodb-csharp

Download to C:MongoCommunityCSharpDriver

You can drop the 3 .DLLs into your .Net project.

                MongoDB.Driver.dll

                MongoDB.GridFS.dll

                MongoDB.Linq.dll

.Net Sample Web Application

Create a new web application in Visual Studio 2010 (or 2008)

Add the 3 reference DLLs.

Create/Open a Default.aspx.cs file and connect to your test Mongo Database:

Drop some controls on (I’m using Telerik’s RadGrid in this example)

    <form id="form1" runat="server">
    <div>
            Create a new Mongo Database:<br />
        Name:
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
&nbsp;<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Create" />
        <br />
        <br />
        <asp:Button ID="Button2" runat="server" onclick="Button2_Click" 
            Text="View Results" />
        <br />
        Results<telerik:RadGrid ID="RadGrid1" runat="server">
        </telerik:RadGrid>

    </div>
    <telerik:RadScriptManager ID="RadScriptManager1" Runat="server">
    </telerik:RadScriptManager>
    </form>

 

Add some code:

        protected void CreateMongoDB(String dbName)
        {
            //mongod.exe must be running for this to work.
            var mongo = new Mongo();
            mongo.Connect();

            //if the database is not found in c:datadb it will be created.
            var db = mongo.GetDatabase(dbName);

            //declare a new "table"
            var categories = db.GetCollection("categories");

            //create a new key value set
            var document = new Document();
            document["Name"] = "Product";
            document["Name"] = "Price";

            //create the "tabt"
            categories.Insert(document);
            mongo.Disconnect();
        }

 

And the button call:

        protected void Button1_Click(object sender,EventArgs e)
        {
            String name = TextBox1.Text;
            CreateMongoDB(name);
        }

 

Run it.

Now you can create MongoDB’s through a web form.

Filling a Grid with the Results

We’ll jump ahead a tiny bit to where you’ve added some documents to one of your MongoDB’s.

Mine is called TestProducts.

Each document has a Name and a Price.

Add some more code to default.aspx.cs

        protected String GetMongoDBResults(String dbName)
        {
            //mongod.exe must be running for this to work.
            var mongo = new Mongo();
            mongo.Connect();

            //if the database is not found in c:datadb it will be created.
            var db = mongo.GetDatabase(dbName);

            //declare a new "table"
            var categories = db.GetCollection("categories");

            //get the categories table
            var category = categories.FindOne(new Document() { { "Name","Price" } });
            //alternatively you can get all documents in the database
            var documents = categories.FindAll().Documents;
            String res = category["Name"].ToString();

            //diconnect
            mongo.Disconnect();

            //return results in a EF friendly shapre just because that's what I'm working with mostly nowadays
            return res;
        }

 

Add the button call code:

        protected void Button2_Click(object sender,EventArgs e)
        {
            RadGrid1.DataSource = GetMongoDBResults(TextBox1.Text);
            RadGrid1.DataBind();
        }

 

Run it.

Now you can fill a Grid with data from your MongoDB.

© Copyright Duke Hall - Designed by Pexeto