Wednesday, September 5, 2012

Get started with MongoDB in windows with .NET

MongoDB is currently one of the most popular NoSQL databases. The idea behind this post is to provide a concise document that will help you get started with MongoDB on Windows with .NET.

Follow the step by step guide below to get started:
  • Download the database server from http://www.mongodb.org/downloads. It’s available for 32 and 64 bit machines. Note that the 32-bit builds can store maximum of around 2 GB data. Refer to  http://blog.mongodb.org/post/137788967/32-bit-limitations for details.
  • The 32-bit installation is of around 18 MB size. It is a zip file which contains a bunch of executables.
  • To start the server:
    •  Create a batch file with 'mongod.exe --dbpath “C:\\data\\d”. Here 'dbpath' specifies the location of the database as "C:\data\db" and any newly created database will be placed at this location.
    • Executing this command will start the server and this is how it  is shown in the console.
Now, let’s take a look at the available drivers in C# .Net for MongoDB. The official driver can be downloaded from  https://github.com/mongodb/mongo-csharp-driver/downloads and its details are at  http://www.mongodb.org/display/DOCS/CSharp+Language+Center. You can also get drivers with fluent interface on top of this official driver from https://github.com/craiggwilson/fluent-mongo. I found another driver-NoRM at https://github.com/atheken/NoRM which uses C# classes. MongoVUE is the IDE available for windows.

Sample Application:
  • Create a console application. This will keep the application free from any other code and everyone can relate to it easily. 
  •  I will be using the NoRM driver in the sample application.  Add the NoRM.dll in your project reference. Add this line along with other statements for using a library: using Norm; 
  • To obtain a handle to the database, a static method is available which needs a connection string to the database. Use the following code: IMongo db=Mongo.Create("mongodb://localhost/MongoTest"); Here the 'MongoTest' is the name of the database and I am using the server on my local machine. 
  • Assuming that the server is up and running, this call will return a valid handle to the 'MongoTest' database. This is valid even if the database is not created yet. A subsequent call to save any value in it can create a new database. Only calling this method will not create a database. 
  • So, let’s assume that we have employee data to be saved. This is how you save an object of employee class: db.GetCollection<Employee>().Save(emp); 'emp' is the object of class 'Employee' and executing this line will actually create a database (if not created already). It will also create a new collection name 'Employee' with one entry for 'emp' object. 
  •  To get a list of employees: var employees = db.GetCollection<Employee>().AsQueryable().AsEnumerable()
  • To delete an employee: Employee emp = db.GetCollection<Employee ().AsQueryable().Where(e => e.Name == "emp1").SingleOrDefault(); db.GetCollection<Employee>().Delete(emp);

Yes, that's all you got to do to get started with MongoDB from .NET. The full application is available at https://github.com/vipul15184/MongoTestConsoleApp and this is the full page of program.cs file to consolidate:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Norm;


namespace MongoTestConsoleApp
{
    public class Employee
    {
        [MongoIdentifier]
        public ObjectId Id { get; set; }


        public string Name { get; set; }
    }


    class Program
    {
        static void Main(string[] args)
        {
            Employee emp = new Employee();
            emp.Name = "emp1";


            IMongo db = Mongo.Create("mongodb://localhost/MongoTest");


            db.GetCollection<Employee>().Save(emp);
        }
   }
}

Labels: , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home