Post1

MongoDB with C#.Net

Posted by

Being a C# developer we closely work with relational databases like MS SQL and Oracle. It thus becomes very exciting to explore the world of a prominent NoSQL database like MongoDB. But initially when I started, I found it hard to get code samples using a new C# driver, so I decided to write this blog and take you on this journey, starting with how to connect to Mongo DB and later on doing some quick CRUD operations using C#.Net driver.

Introduction

MongoDB is an open source “schema less” document database. “Schema less” is the key here, let’s see how.

MongoDB is a JSON style data storage, it keeps all of your information with a variable set of fields and with different type for each of them. These attributes allow MongoDB to be a high performance, high availability, and automatic scaling database with capabilities to perform dynamic queries.

Let’s come back to our topic and see what all do we need in MongoDB and Visual studio 2015. I will not take you into the steps of setting up the environment as you can find those at all the places easily.

Starting with C# project:

Running Mongod.exe from your installation folder will start MongoDB instance (preferably on localhost, port 27017). Once your DB is running, let’s create a sample console application in Visual Studio 2015 and connect that to MongoDB using C# driver.

So once you are ready just follow the steps below and it should be a piece of cake henceforth;

  1. Create a console application. Name it, say, SampleMongoDB.
  2. Install MongoDB C# driver from Tools menuàNuGet Package Manager à Manage NuGet Package for Solutions à Browse à Search “MongoDB” à Install MongoDB.Driver.
  3. This will add MongoDB DLLs to our application.

Create Connection with MongoDB from C#:

MongoDB driver provides MongoClient class to connect MongoDB instance. Connection can be made by passing database connection string.

MongoClient client = new MongoClient(“mongodb://localhost:27017”);

In MongoDB, tables are called documents that are similar JSON objects. Next, we connect with database and create a collection. Collection is similar to a Table in RDBMS.

IMongoDatabase db = client.GetDatabase(“DemoDB”);

IMongoCollection<Person> obj = db.GetCollection<Person>(“Person”);

The Person class has following structure:

public class Person

{

public string Name { get; set; }

public string City { get; set; }

public int Age { get; set; }

}

Insert Operation:

The document can be added to collection as –

MongoClient client = new MongoClient(“mongodb://localhost:27017”);

IMongoDatabase db = client.GetDatabase(“DemoDB”);

IMongoCollection<Person> obj = db.GetCollection<Person>(“Person”);

obj.InsertOne(new Person { Name=”Amit”, City=”Noida”, Age=32 });

MongoDB provides the following methods for inserting document into a collection:

 

Update Operation:

The document can be updated as

IMongoCollection<Person> obj = db.GetCollection<Person>(“Person”);

var filter = Builders<Person>.Filter.Eq(s => s.Name, “Amit”);

obj.ReplaceOne(filter, new Person{Name=”Akash”, City=”Noida”, Age=30});

Builder class is used to build query expression and ReplaceOne method to update a single document in collection. MongoDB provides the following methods for updating:

Delete Operation:

The document can be deleted as

IMongoCollection<Person> obj = db.GetCollection<Person>(“Person”);

var filter = Builders<Person>.Filter.Eq(s => s.Name, “Amit”);

obj.DeleteOne(filter);

DeleteOne method is used to delete a single document from collection. MongoDB provides the following methods for deleting documents.

Read Operation:

The documents can be fetched as

IMongoCollection<Person> obj = db.GetCollection<Person>(“Person”);

IList<Person> lst = obj.Find<Person>(new BsonDocument()).ToList();

Find method is used to read collection document. BsonDocument class is used to provide filters.

MongoDB provides the following methods for reading documents.

Don’t go only by the short example shared here, MongoDB driver provides many different ways to use its document database, giving us various options to explore. Today in this blog, I gave you a few quick steps for an easier understanding and I believe this will encourage you to do some digging on your own to explore the various other features.

Any thoughts, suggestions or questions are always welcome!

Connect in the comment section and let’s get this conversation going!

 

Related Posts

  • Oracle Goldengate

    Oracle GoldenGate is an Oracle proprietary software for real-time data integration and replication that supports different databases- Oracle, DB2, SQL Server, Ingres, MySQL etc. Even the source and target database…

  • Understanding Oracle Multitenant 12c database

    Overview of Oracle Multitenant Databases Overview Database 12c Enterprise Edition introduces Multitenant, a new design that empowers clients to effortlessly merge numerous databases, without changing their applications. This new design…

  • Content Data Store

    Content Data Store Content Data Store (CDS) is a system to provide storage facilities to massive data sets in the form of images, pdfs, documents and scanned documents. This dataset…

  • Working with NoSQL Database – Apache CassandraWorking with NoSQL Database – Apache Cassandra

    Today, every piece of information, whether it is in simple text or is a large document or image, holds some importance for businesses and needs to be stored. While most…

  • Mobile App Automation Testing using ‘ESPRESSO’

    If you are a Mobile Apps Test Engineer, you cannot overlook the very reliable Google Product i.e. Espresso. Espresso is an automatic UI testing or as we call it “hands…

  • Teradata and JSON – Monetizing the Internet of Things (IoT)Teradata and JSON – Monetizing the Internet of Things (IoT)

    The prevalent influence of technology has resulted in a widespread use of a variety of devices such as cell phones, sensors, web API’s and browsers. These devices generate huge volume…

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *