Friday 31 July 2015

Using MongoDB with C# driver


In this blog post we will understand how to use MongoDB as backend service for our .NET application using C# drivers.

MongoDB
As we know that MongoDB is a NoSQL (i.e. non-relational) database, which is an open source DBMS stores data in form of documents with JSON format. NoSQL databases provides high performance and it is easy to maintain in comparison to any RDBMS (Relational database management system) when we have to deal with unstructured data for our application.

Configure MongoDB & Robomongo

To use MongoDB within our .NET application first we need to install MongoDB and Robomongo(Free GUI tool to use MongoDB) into our system. This is very easy process to install, configure and use MongoDB.

Steps to configure MongoDB

1.       First we have to download MongoDB. While downloading it, make sure we choose correct version for windows (i.e. download should fit our system configuration, as for Windows 8 on a 64 bit processor we need “64 bit 2008 R2+”). Using msi file we need to install MongoDB.
2.       Make entry inside environment variables: (PATH variable)
C:\Program Files\MongoDB\Server\3.0\bin\”.
3.       Default installation location for this will be:
C:\Program Files\MongoDB\Server\3.0
4.       Create a folder named MongoDB at any location (prefer to create at root level)
For example: D:\MongoDB
5.       In the folder created above we need to create a folder “data” and inside data folder create another folder “db”. 
6.       Inside MongoDB folder create a text file, name it to “mongod.cfg” and paste following code there:

dbpath = D:\MongoDB\data\db
logpath = D:\MongoDB\mongod.log
logappend = true

Note: dbpath & logpath can be changed as per path for MongoDB  folder in system

7.       Create window service, run the command prompt as “Run as administrator”. Go to the path to bin folder of MongoDB in our system (i.e. C:\Program Files\MongoDB).
8.       Run following command:

sc.exe create MongoDB binPath= "C:\Program Files\MongoDB\Server\3.0\bin\mongod.exe --service --config=\"D:\MongoDB\mongod.cfg\"" DisplayName= "MongoDB" start= "auto"

After this MongoDB is up and running on our system as a service.


Steps to configure Robomongo(GUI tool)

1.       First download robomongo and install it into your system.
2.       Start robomongo tool and try to configure database. Inside MongoDB connection wizard click “create” link to establish connection.


Let’s start some coding

Up to this point we have MongoDB and Robomongo installed into our system. Now this is the time to use MongoDB as DBMS for our application using C# drivers.
1.    Make sure visual studio installed into system. Open visual studio and create a sample console application, name it to : “LibraryApplication
2.       Second step is to install official mongoDB C# drivers’ packages from nuget store. Go to online nuget packages and type “mongo”. Install both drivers for application.


3.       Check this step will install few dlls into application:


MongoDB.Bson, dll deals with BSON document of MongoDB and provides information how to serialize data from that document.
4.       The first thing , I am going to create a class for our LibraryApplication project:
public class BookStore
    {
       public ObjectId Id { get; set; }
       public string BookTitle { get; set; }
       public string Auther { get; set; }
       public string Category { get; set; }
       public string ISBN { get; set; }
    }
Here notice I have created one property with type “ObjectId” and name “Id”. This is the unique identification for a document in MongoDB. We can have this type with any other unique property as well. In fact if we will not create any of such property in class, then MongoDB by default create the same with name _Id, to uniquely identify that document.
5.       Other step is to establish connection to database (MongoDB) and try to insert data to our database.
static void Main(string[] args)
{
            MongoClient client = new MongoClient();
            var server = client.GetServer();
            var db = server.GetDatabase("LibraryDB");
            var collection = db.GetCollection<BookStore>("BookStore");

            BookStore bookStore = new BookStore
            {
                BookTitle = "MongoDB Basics",
                ISBN = "8767687689898yu",
                Auther = "Tanya",
                Category = "NoSQL DBMS"
            };

            collection.Save(bookStore);
    }
a.  Check the first line of code, “MongoClient” is used to create a connection to MongoDB. We can also specify the connection string (with server name, port etc.) to this object. For now I am connecting to local database server with default port, so don’t need to provide connection string argument here.
b.  Next line is to get the server connected.
c.  Once server has been connected, then we have to get the database.
d.  Next is to create a collection (can be think of as a table in RDBMS) and save data into it.

6.       After executing the program we can see the magic:
 
In our database, a new database named “LibraryDB” has been created with collection “BookStore” and data inserted in it.

Important feature here is, it will create new database if not exists and update if already exists. Same is with collections.

For example if we add another record into a collection:
BookStore bookStore = new BookStore
       {
              BookTitle = "C# Basics",
              ISBN = "27758987689898yu",
              Auther = "Somya",
              Category = "Programming Languages"
};
This will add one more document to existing “Bookstore “collection.

7.       One more important thing is, as I told earlier we can also set any of our property as unique identification like this:
[BsonId]
public string ISBN { get; set; }

Run the application and check, now ISBN number will act as unique identification for document. There will not be any field name ISBN in database in this case.


8.       We can have nullable fields in MongoDB and that is not shown in document if value is null:
[BsonIgnoreIfNull]
public int? TotalPages { get; set; }



9.       Now let’s query the data from database:

To get books count having pages more than 200
collection.AsQueryable().Where(b => b.TotalPages > 200);

To get Book having title starts with 'Mongo’
collection.AsQueryable().Where(b => b.BookTitle.StartsWith("Mongo"));

To get Cheapest Book
collection.AsQueryable().OrderBy(b => b.Price).First();

To get book with a particular ISBN number
collection.AsQueryable().Single(b => b.ISBN == "27758987689898yu");

To remove all collection
collection.RemoveAll();

Summary

So in this article we understood how to use MongoDB in our .NET applications using C# drivers. How we can insert/ query data from MongoDB using C# code.
Please stay tuned for more information on MongoDB usage.


Monday 27 July 2015

Getting Started with MongoDB

In continuation to my previous blog post "MongoDB Basics" which gave an idea about what NoSQL DBMS is and what are the types and why we should use them, this blog also provides some further knowledge about how we can get start using MongoDB with GUI tool and take advantage as back-end service for our application development.

MongoDB

MongoDB is a NoSQL (i.e. non-relational) database. It is an open source DBMS which stores data in form of documents with JSON format. NoSQL databases provides high performance, scalability in comparison to any RDBMS (Relational database management system) when we have to deal with unstructured data in our application.

MongoDB is one of the popular NoSQL DBMS in comparison to others. It stores data inside documents (Objects) in form of JOSN format which makes faster performance and easily deal with datatypes of any programming language. MongoDB is beneficial as it provides some features same as any RDBMS like indexing, replication, sharding etc. In such way we can leverage the dual benefits as we are able to use features which we used to have with RDBMS with the other benefits of non-relational databases as well.

The most common use of NoSQL DBMSs’ are:
  •  To store real time data like: sensor based applications’ data.
  •  To store large amount of unstructured data in form of documents.
  • To maintain/archive old blog post data.
  • To maintain large amount of “Product catalog/Inventory” data, this is not relational in nature. Hence it is best to use NoSQL DB for this purpose.
  • Real time based applications.
  • To store comments from users for an e-commerce site.
  • Online gaming.

Configure MongoDB

This is very easy process to install, configure and use MongoDB.

Steps
  1. First we have to download MongoDB from official site:

http://mongodb.org/ and download Mongo.  While downloading it, make sure we choose correct version for windows (i.e. download should fit our system configuration, as for Windows 8 on a 64 bit processor we want “64 bit 2008 R2+”). This will provide msi file which we need to install
2.   Make sure entry inside environment variables: (inside PATH variable)

          “C:\Program Files\MongoDB\Server\3.0\bin\”.
      3.  After installation, check it will default store files inside our program files directory:
C:\Program Files\MongoDB\Server\3.0
            4.  Now we need to create a folder named MongoDB at any location (prefer to create at root level)
For example: D:\MongoDB
                       5.  In the folder created above we need to create a folder “data” and inside data folder create another folder “db”. 
            6.   Inside MongoDB folder create a text file, name it to “mongod.cfg” and paste following code there:

dbpath = D:\MongoDB\data\db
logpath = D:\MongoDB\mongod.log
logappend = true

Note: dbpath & logpath can be changed as per path in our system

            7.   To create service, run the command prompt as “Run as administrator”. Go to the path to bin folder of MongoDB in our system (i.e. C:\Program Files\MongoDB)
 Run this command:

sc.exe create MongoDB binPath= "C:\Program Files\MongoDB\Server\3.0\bin\mongod.exe --service --config=\"D:\MongoDB\mongod.cfg\"" DisplayName= "MongoDB" start= "auto"

After this MongoDB is up and running on our system as a service.




GUI tool to use MongoDB(Robomongo)

To easily use mongoDB we have GUI tool available.
First download it from official website: (Windows Installer)

1.       We need to install this tool into our system.
2.       Start robomongo tool and try to configure database. Inside MongoDB connection wizard click “create” link to establish connection.




3.       Here we can create connection to server by providing user authentication details inside “Authentication” tab and can provide default database name inside “Advanced” tab.
4.     After connecting to database server, we need to create our database. Right click on “New Connection” and select “Create Database”. Provide name to DB for example: “PeopleDB


 5.   This step create empty database and we have to then create collection, by right clicking on collections and select “Create Collection”. Provide name for example: “People”.
Note: For RDBMS folks it can be think of as a table.
6.    To get any data from collection we need to double click on collection name.
      7.     Now we need to insert some data to our People collection. By double clicking on it we can find shell script open at right side of window.
            8.     We need to write insert command there and press run. This will insert 1 record in our DB.


        9.     Double click on collection and see newly added data.


      10.    Here notice one additional field that has been added is _id and every document in MongoDB contains this id by default. We can have our own as well.
      11.   Now I am going to add one more record. As MongoDB is schema less so we can provide more information also.


      12.   Here notice power of MongoDB, as if we have to add some information to our table then we have to either add new column for that or create some other relational table but here we can easily add such data to document(as we don’t need to respect schema).
      13.   One more benefit of this tool is we can easily edit/ add data by right clicking on any document (record) and select “Edit document”.


      14.   Using mongoDB, we can have nested documents (i.e. document inside another document). This solves our problem as in SQL we had to create another table for this purpose.


      15.   This was the insertion of data. Now we have to query this inserted data. For example: If I have to find data with name: Tanya, I have to provide this condition inside find function.



     16.  Ordering data: We can also get data in specific order by providing sort function:

To sort in ascending order give 1:
db.People.find().sort({name:1})

To sort in descending order give -1:
db.People.find().sort({name: -1})

      17.  Using “equal/ greater than/ less than” operators:

To get record with age=25:
db.People.find({age:25})

To get record with age>18:
db.People.find({age:{$gt:18} })

To get record with age<18:
db.People.find({age:{$lt:18} })

Summary

So in this article we understood what MongoDB is, why we should use it and how we can start using it by GUI tool and how can we store data into it and query that data.
Please stay tuned for more information about how we can use MongoDB from C# application using C# driver.



Saturday 25 July 2015

MongoDB Basics

Introduction
In this blog post we will understand what is NoSQL databases and why we should use them for our application development.

NoSQL Database
A NoSQL (referred as "non SQL") database provides a mechanism for storage and retrieval of data in form of structure which is other than the tabular relations used in traditional relational databases.

Unlike a RDBMS(Relational database management system) , NoSQL database(Non relational database) structure stores data in form of key/value pair, document and graphs etc.

Why NoSQL Database over RDBMS
When we are working in a culture to use relational database to store our data in form of tables with relationship maintained among them, then why we should use a non relational database or what is the need to use NoSQL Dbs as backend providers for our applications. Here is the answer:
  • NoSQL databases solve scalability and performance issues that relational databases weren’t designed to address
  • NoSQL is especially relevant when data is unstructured, unpredictable or needs to be massively processed in real time. For example:  "sensor based applications".
  • As a database grows in size or the number of users multiplies, many RDBMS-based sites suffer serious performance issues.

MongoDB
There are several NoSQL database structures are available, but MongoDB is one of the leading NoSQL database among them.
MongoDB is a open-source document database that provides high performance, high availability, and easy/automatic scaling.

Document Database
A record in MongoDB is a document(object), which is a data structure composed of field and value pairs and map easily to programming language data types. MongoDB documents are similar to JSON objects. The values of fields may include other documents, arrays, and arrays of documents which reduce need for joins.
{
product: "Maggi",
price: "78.0",
category: ["Food","Noodles"]
}

Key Features
There  are several key features provided by MongoDB which makes it best choice among other NoSQL Dbs for application development.

MongoDB supports almost all the languages and there are lists of drivers available for MongoDB and it open sourced at Github at following location:

There are several reason why people should choose MongoDB over other relations databases:

High Performance:
  • MongoDB stores data in JSON format so that performance is very fast.
  • It supports "Indexing" , which results in faster querying to database and can include keys from embedded documents and array.

Easy/Automatic Scalability:
  • It is easy to install, configure, maintain and use.
  • In RDBMS we need special database administrators to maintain data in database, while in MongoDB its very  to manage and with very few command it will provide all the functionalities.
  • MognoDB supports automatic sharding and you can use shard data at multiple locations.
  • It is possible to increase capacity without any downtime, which is very important on the web development when load can increase suddenly and bring down the website for maintenance can cost very high for business.

Flexibility:
  • We can store data in a way we want there is no restriction on database schema(unlike RDBMS). 
  • It works on any operating system so we can use it on any platform that our application requires.
  • It supports all languages.

Replication:
MongoDB also supports replication just like relational databases. It supports replication with replica sets. Each replica set contains two or more copies of the data. There are two types of replica set:

Primary Replica
Between two set of replicas, one replica act as Primary replica which performs read and write operation.

Secondary Replica
Other replica act as secondary replica set which create copy of data on top of Primary replica set.
Main goal of secondary replica set is, if primary replica set fails it start acting as primary and performs all operations of primary.
In such a way a replica set which is a group of MongoDB servers, maintain the same data set to provide redundancy and increasing data availability.

Power:
MongoDB provides lot of features of a traditional RDBMS such as :-
  • Secondary indexes,
  • Dynamic queries,
  • Sorting,
  • Rich updates, upserts (update if document exists, insert if it doesn’t), and easy aggregation.
With this we can easily relate functionalities which we use from RDBMS, with the flexibility and scaling capability that the non-relational model provides.

Use case scenarios
Now with the above explained details we have clear understanding of why we should opt for MongoDB as backend for web application development. Here are some example of applications where we use this:
  • Product Catalog
  • Inventory Management
  • Meta data/ asset management.
  • Storing comments
  • Archiving data (most famous implementation for mongoDB is : to archive old blog posts)
  • Sensor based data store
  • Online gaming
  • Logs Centralization (Asynchronous & speedy)
  • Network based queuing (distributed message-passing) implementations.

NoSQL DBMS types
There are several types of NoSQL databases are available:

Key / Value based:
Key / Value data stores are highly performant, easy to work with and they usually scale well.
  • Redis
  • Riak
  • Memcached / MemcacheDB
When to use
  • Caching
  • Queuing
  • To keep live information
  • For distributing information

Column based:
Column based data stores are extremely powerful and they can be reliably used to keep important data of very large sizes.
  • Cassandra
  • HBase
When to use
  • To keep unstructured, non-volatile information
  • To keep data stores which are highly scalable in nature.

Document based:
Document based data stores are excellent for keeping a lot of unrelated complex information that is highly variable in terms of structure from one another.
  • MongoDB
  • CouchDB
  • CouchBase
When to use
  • To keep nested information
  • To keep javascript friendly data stores.

Graph based:
Graph based data stores offer a very unique functionality that is unmatched with any other DBMSs.
  • Neo4j
  • OrientDB
When to use
  • Modelling and handling classifications
  • Handling complex relational information

Summary
In above article we learned about basics of NoSQL database management systems and why we should use them.
What are the key features provided by MongoDB which makes it best fit for web application development.
Please stay tuned for information about how to get start with or use MongoDB.