MongoDB with CSharp

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 would have gone through MongoDB on Windows quick start in 5 minute then you know how to start MongoDB server. However just for quick recap you need to start MongoDB server as below,

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

image

By default MongoDB stores data inside data folder of 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.

image

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

Create Project and perform operations

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

Create DataBase

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

clip_image002

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

Add Record in DataBase

You can add record as below,

clip_image002[6]

Where blogger is collection you get over database as below,

clip_image004

Delete Record from DataBase

image

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

image

Fetch a Record

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

image

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

For your reference 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 post is useful. Thanks for reading.

3 responses to “MongoDB with CSharp”

  1. hello sir,

    i was searching the forum for a utility/script, which can catch real time stock quotes from an open excel file and feed it to charting software, Amibroker.
    I read your post in other forum about reading realtime data from excel, but i dont have programing skills, so i cant understand much.
    Please help

    Regards

  2. Just an FYI: the driver you have pointed to and subsequently used is extremely old and outdated (it isn’t even the newest version of that driver either). The official driver at https://github.com/mongodb/mongo-csharp-driver. Alternatively, it is available on nuget at http://www.nuget.org/packages/mongocsharpdriver.

Leave a comment

Create a website or blog at WordPress.com