Getting started with MongoDb

Kailash Chandra Behera | Wednesday, September 14, 2016

Introduction

The main goal of this article to explains how to work with MongoDB using C Sharp. will help you to learn create database, create database, create collection, insert document into collection, remove document from collection and query from collection etc. To work with MongoDB you need to download MongoDB C sharp driver that supports to work with MongoDB.

Getting Started

Working with Mongo DB is the same as working in Linq and entity framework. Mongo DB contains same feature, same work style that Linq and Entity FrameWork have. This is one of the great features of mongo DB.

First download the driver then add the reference of two dlls names (MogoDB BOSON and MongoDbdriver) and use the reference of following namespaces.

 using MongoDB.Bson;  
 using MongoDB.Driver;  
 using MongoDB.Driver.Builders;  
 using MongoDB.Driver.GridFS;  
 using MongoDB.Driver.Linq;  

Connection String Syntax

Default Connection String: "mongodb://localhost"
Above connection string will connection to local default server having port 27017, you can use customized connection like "mongodb://192.162.1.xx:27017"

Creating Client

 MongoClient client = new MongoClient(connectionString);  
Like SQL connection you are required you create Mongo Db client,above code creats object of Mongoclient which takes connection strig as parameter.

Getting Server Reference

 MongoServer server = client.GetServer();  
Above code gets reference of mongo server, its calls GetServer() function of mongo client that returns objects of mongo server;

Getting Database Reference

Create mongo database to get collection (Table in Sql server), where you store your.
 MongoDatabase database = server.GetDatabase("Test");  
This code calls GetDatabase function to get reference of mongo databse . the Get Database function takes name of server as parameter, if database not availabel Mongo DB creates a database with specified name in the parameter and returns the database . Here code trying to get reference of test database.

Getting Reference of Collection

Like SQL server mongo db database contains collection to store data. To get reference of collection, it is required to call get collection function of mongo db database.
 MongoCollection symbolcollection= database.GetCollection<Symbol>("Symbols")  
The getcollection function takes symbol name as parameter and returns reference of collection, like get database collection it also create if collection not available specified name in the parameter it creates a collection having same name. Here symbol is an entity that specifies type of collection.

Getting Reference of Collection List

List of mongo collection can also be get by calling GetCollections function of database, it return all collection available in the database in list.
 List<MongoCollection> collections=database.GetCollections();  

Drop Collection from Database

 database.DropCollection("Symbols");  
Drop Collection function of mongo database class deletes collection specified the name in the parameter from database.

Insert into Collection

 Symbol symbol = new Symbol ();  
 symbol.Name = “Star”;  
 collections.Insert(symbol);  
symbol is a class, above code inserts an object of symbol into collection. In below mention you would have noticed id which that type is ObjectID. Property id is not mentioned in the above code because this is optional, if you do not mention mongo db collection like SQL table automatically gives an id number to it. Like auto generated id column in the SQL server table.
 public class Symbol  
  {  
    public string Name { get; set; }  
    public ObjectId ID { get; set; }  
  }  

Querying form Collection

 List< Symbol > query = symbolcollection.AsQueryable<Symbol>().Where<Entity>(sb => sb.Name == "Star").ToList();    

Summary

In this article we have discussed how to work in mongodb using C#, Hope this article may helpful to you

Thanks