Saturday 1 February 2014

Term Comparison: MongoDB vs. RDBMS

Today's discussion will be a fun my friends as we will learn few nomenclature of MongoDB so that our learning will get some pace.

I would request you to quickly recap our first day discussion for configuring MongoDB. After that it is assumed that MongoDB is up and running in your local environment.

In your mongo shell (assuming your have just opened it) type the following command:

> db

This will give you the database your are currently connected to.

The mongo shell is a java script shell accepting all the java script command. db is a variable which is used to return the current database you are connected to.

RDBMS Database = MongoDB Database

Database is a physical unit of file in MongoDB which contains data grouped in logically in an application. Once the user starts inserting some data into a DB it will create a physical file with the same name of the database at the data folder (remember we have a data folder at the time of start up). Please note we mention "start inserting" that means till the time there is no record in a database there is no physical file created. 

To see what all databases are present in the running MongoDB instance, type the following command:

> show dbs




This will list down all the databases with their corresponding size (see above image). To connect to some other database type 

> use [db name]





Now let say you want to connect to a database which doesn't exist. MongoDB will not restrict you to do so. It will logically create a variable and connect you. For example if you don't have a database called blog (show dbs command doesn't return any name called blog) and you type use blog, the shell will give you the same result as above. At this time it is just a logical name than a physical entity. To check, go to your data/db folder and you will find NO file with the name blog. Now we will insert some data and see what happens.



RDBMS Table = MongoDB Collection

The highest logical unit inside a database is called a Collection. Collection is same as Table in RDBMS. There is no command like CREATE COLLECTION exists in MongoDB (as opposed to CREATE TABLE command in RDBMS). The first time you insert some record in a collection, it will get created. Let say we want to insert some record in a collection called user in the blog database. Before that lets find whether the user collection exists or not. To check the same, type:

> show collections

The shell returns nothing as there is no collections in the blog database. Now insert a record into the user collection using the following command:

> db.user.insert({"firstName" : "Roger", "lastName" : "Federar", "plays" : "Tennis" })

Now you type the same command for showing collections, see the result:





Also to list the databases please type show dbs and see the result now. The blog database is listed down. Now you go to the data/db folder. You should find the following three files created for the blog database:

blog.0
blog.1
blog.ns

Note: You see there is no CREATE DATABASE command exists either ... cool right?

Row/Column = Document/Field

The concept of a row in RDBMS is replaced here as Document (JSON formatted). As you see in the previous example a document is a java script object. For example:

{"firstName" : "Roger", "lastName" : "Federar", "plays" : "Tennis"}

This is an object with three fields (Key-Value pair) namely - firstName, lastName and plays. The keys of the document is always a string while the value can be of several types. MongoDB supports null, boolean, number, string, date, array, embedded document, Object Id, binary data, code as the value of a key. We will go over each type in the next discussion.

Index = Index


The concept of indexing is similar here as in case of RDBMS. Actually MongoDB support various kinds of indexes (as mentioned in the previous discussion). Basically it does indexing using B-Tree indexes. There will be detailed discussion later with indexing in MongoDB.

Foreign Key = Reference

MongoDB doesn't support referential integrity, though the reference of one field of one collection is declared as @DBRef in another collection (we will discuss this as well in future post, just want to keep things simple).



<< Prev                                                                                     Next >>

No comments:

Post a Comment