Monday, November 14, 2011

Mongo DB - schema less database - Series 1

Overview:  

I'm leveraging the new schema less database called Mongo DB and thought write a blog and share my experiences/knowledge to the community.  I'm creating a series of post to explaining  from  overview to install to implementation using both Python as well as from C#.

In this post I'm going to give you an overview and explain installation option and some silent feature.

Series 1 : Overview, Install and key feature

What is Mango DB?

A MongoDB database is non-relational and schema less database and its designed based on document storage concept. This means that a MongoDB database isn't bound to any predefined rows, columns, constraints,  data types, etc... the way that traditional relational databases are (Oracle, SQL-Server, DB2, MYSQL, etc…).

The philosophy in which the MongoDB  was designed is "one size does not fit all".  What is this means in reality?  It is simple , lets say we have a situation where we need to store different data attributes based on a type of document (RDBMS: Rows).  

example:

We have a table called Audit and based on the audit type one row can have a type that is defined as a string and another type can be Date Time and another one may have more than one value (a collection of data), this is impossible in RDBMS from a single table.  We need to normalize  and every time there is a need for another type of collection, we may need to build another table, in contrast MongoDB or any other Document DB, it is simple since they are based on the key value pair.  The key is a string and the value in it can be anything.  That’s the beauty of this DB.  Is it a replacement for Relational Database?  The answer is a big 'No', it depends on the need or complexity.


How does I query the data? 
It is really simple, since MongoDB is all about  document Object type,  JSON  is the ideal choices. 

JSON  (Java Script Object Notation)  is a lightweight data-interchange format, which is completely independent from programming languages.  JSON primarily works with two different structured data:
  • A collection of name/value pairs.
  • An ordered list of values.


To learn more about JSON, visit : http://www.json.org/

BSON (Binary Java Script Object Notation):
MongoDB doesn't actually use JSON to store the data; rather, it uses an open data format developed by the MongoDB team called BSON.  BSON makes MongoDB even faster by making it much easier for a computer to process and search documents. BSON also adds a couple of features that aren't available in standard JSON, including the ability to add types for handling binary data.

Does this mean learning a new Query Language?  No, if you are familiar with JSON, it is straightforward,  BSON is built on top of JSON and supports current JSON query on top of few additional features that are specific to binary data.

How does the MongoDB actually stores data?  It is simple as a document with a  keys and values, it generated an uniquely identifier key and assigns to each document, unless we specify a unique key field.  Think this is similar to RowID in Oracle or  ROW_NUMBER()  in SQL server.


Installing MongoDB under Windows

Prerequisites

Before we can start the MongoDB service, we need to create a data directory for MongoDB to
store its files in. The following folders  are configured to use by default by MongoDB

C:\data\db directory

But, they are not created by default, so we need to create them manually; otherwise, MongoDB will fail to run and throw an error message.

Once the folders are created set the read, write, and directory creation permissions to function properly.

Note: If you wish to use a directory other than the default , then we need to  tell MongoDB to look at the custom  directory by using the "dbpath" flag when executing the service.

Download & Install

Select the latest windows build (2.0.1) of MongoDB from http://www.mongodb.org/downloads
After you download the Zip file, extract the zip file and drop it in a location you prefer to,  I like all the software's to be located under C:\Program files directory

C:\Program Files\mongodb-win32-i386-2.0.1

Once you extracted the zip file and moved to preferred location, you'll see the following directory structure

 
Under the bin directory where you'll see bunch of executable  (these are the only executable' s we need to run the entire database engine)




The main executable are listed below

mongod.exe
core database server
mongo.exe
Database shell
mongorestore.exe
Utility to import /restore
mongodump.exe
Utility to export/backup
mongos.exe 
auto-sharding process


Installing additional drivers for application integration

Currently, MongoDB includes drivers for the following programming languages and can be accessible from


As I mentioned earlier in the blog , I'm trying to leverage my development using C# as well as Python, I'm showing here the download and install instructions for the same technologies:


.Net Driver (C#): is very straightforward:  Just download the Microsoft installer version (.msi file) from  http://github.com/mongodb/mongo-csharp-driver/downloads and run  the msi.  This will install the required driver file (C#)  and also the help files


Python Driver: Download pymongo-2.0.1.tar.gz from  http://pypi.python.org/pypi/pymongo/#downloads and install using the 'easy_install' utility, which is available under scripts directory




To confirm Python MongoDB driver is working in python run the following command  from python shell (command line) and should expect no result

import pymongo




in the next  Series 2, we'll see how to start creating database, documents and accessing the same using JSON (BSON object).