ARTICLE

MongoDB With CSharp

Posted by Dhananjay Kumar Articles | C# Language January 23, 2012
In this article we will do a quick start of MongoDB with CSharp.
Reader Level:

Before you start working with MongoDB using C Sharp, I recommend you to read MongoDB on Windows quick start in 5 minute and download MongoDB for CSharp from here.

Start MongoDB server

If you have gone through MongoDB on Windows quick start in 5 minute then you know how to start a MongoDB server. However just for a quick recap, you need to start MongoDB server as below.

You can connect to MongoDB server as below by executing mongod exe:

mango1.gif

By default MongoDB stores data inside a data folder of the C Drive. You need to explicitly create this folder as well.

Add required library in solution

You will get a solution when you download MongoDB for CSharp from here. Open the solution in Visual Studio and build it.

mango2.gif

Take MongoDB.dll and add this reference to your project.

Create Project and perform operations

For purpose of this article, I am going to create a console application. I have added the MongoDB.dll as a reference in the console application project.

Create DataBase

If you want to create a database, you can create as below:

mango3.gif

The above code will connect to the MongoDB and create a database called Bloggers, if it does not exist.

Add Record in DataBase

You can add a record as below:

mango4.gif

Where blogger is collection you get over database as below:

mango5.gif

Delete Record from DataBase

mango6.gif

You can delete a record by just providing one key value as well like below:

mango7.gif

Fetch a Record

To fetch a particular document or record you need to create a document and provide the key value as below:

mango8.gif

The FindOne() function returns a document . You need to call the Get function with a key value as input to fetch the value.

For your reference, the full source code is as below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB;
 
namespace ConsoleApplication34
{
    class Program
    {
        static void Main(string[] args)
        {
 
           //Create Database
            Mongo mongoDBdataBase = new Mongo();
            mongoDBdataBase.Connect();
            var dataBaseToWork = mongoDBdataBase.GetDatabase("Bloggers");
            //Create Collection
            var blogger = dataBaseToWork.GetCollection("blogger");
 
           //Insert Records 
            var b = new Document();
            b["Name"] = "Dhananjay";
            b["Country"] = "India";
            blogger.Insert(b);
            b["Name"] = "G Block";
            b["Country"] = "USA";
            blogger.Insert(b);
 
           //Fetch Record
            var searchBlogger = new Document();
            searchBlogger["Name"] = "Dhananjay";
            var result = blogger.FindOne(searchBlogger);
            Console.WriteLine(result.Get("Country").ToString()); 
            Console.ReadKey(true);
        }
    }
}

In this way you can perform operations on MongoDB using CSharp. I hope this article is useful.

Thanks for reading....

COMMENT USING