Installing MongoDB

In the first post on MongoDB, we had a very brief introduction. MongoDB can be installed on a MAC, Windows and Linux system.

Installation

This tutorial is (for now) focused on Ubuntu. MAC and Windows users can directly download the package from MongoDB Downloads page and follow the instructions.

Installing MondoDB in Ubuntu is as simple as running the following command:

sudo apt-get install mongodb

As mentioned above that Ubuntu already has MongoDB package in its repositories but 10gen packages are more up to date. Following these steps will ensure 10gen’s MongoDB installation:

Public GPG Key of 10gen to ensure authenticity

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10

Add 10gen Repository
Either add the following line to the end of the /etc/apt/sources.list file or create a new file named /etc/apt/sources.list.d/10gen.list with these following contents:

deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen

Update with the new Repository

sudo apt-get update

Finally the actual installation

sudo apt-get install mongodb-10gen

Configure MongoDB

MongoDB on Ubuntu uses /var/lib/mongodb for storing data and /var/log/mongodb/mongodb.log for logging. This can be changed from the configuration file:

sudo vim /etc/mongodb.conf

Additionally the control script of MongoDB is located /etc/init.d/mongodb. The account used to run the MongoDB instance is mongodb user account. The directories mentioned above already have permissions with this account.

Starting & Stopping MongoDB

The service name is mongod for MongoDB>. To start it:

sudo service mongodb start

To stop it:

sudo service mongodb stop

Now that MongoDB is installed and running, we will start with actually using it in the next post.

Advertisements

Introduction to MongoDB

MongoDB Logo

MongoDB Logo

MongoDB belongs to a new brand of databases known as NoSQL Databases or Schema-less Document Oriented Databases. MongoDB was first introduced in 2009 and has been in very active development since then. Lets first see the difference between NoSQL and SQL databases.

How is NoSQL different from SQL Database

Traditional databases a.k.a RDBMS use relations or tables to structure data. Relations in the form of rows and columns. Where row or tuple stores a complete record of an item. A column represents the collection of all same attributes of the records. Then there are Primary Key and Foreign Key relations. Constraints can be added and you have a proper schema running with data types, data length, allow NULL or NOT NULL etc.

NoSQL databases on the other hand simply do not have any schema. Which means no constraint, no limitation on data types, no maximum length issues. The idea of structuring data is very simple, which is key-value storage. These key-value pairs can further be stored in a Document, which is basically a simple collection with different or similar fields. The details will be covered in later posts.

A major attraction is the ability to store data from objects in programming languages directly without a need to map the data members of objects to column names of the table. NoSQL makes storing data for objects in object oriented language very logical and natural unlike RDBMS where a mapping is required. In short Document databases are the right match for Object Oriented Programs with no translation required. Apart from object oriented tasks, another reason is that many times our data is not always can be structured in the form of rows and columns. Previously designers had to make an artificial structure just to store their data in RDBMS.

NoSQL is good for larger public websites where the user base is very large and the rate of data read and write is very high. Such websites need to grow with time that is they have to be very scalable. Replication is built right into them. A reason for this is that RDBMS databases primarily were developed in the era where data was to be stored on large single servers. This has changed a lot now.

Examples of NoSQL are MongoDB, CouchDB, HBase, Cassandra, Hypertable etc.

So Which One Wins?

This understanding is not correct that NoSQL is here to replace SQL. No that is not the case, in fact NoSQL databases’ application area is where SQL was not good at. The inspiration of NoSQL databases comes from Google’s BigTable and Amazon’s S3 which are used by these companies for their own storage purpose.

SQL databases still have their strengths and will remain.

MongoDB as an NoSQL Database

It uses JSON format to store all of the data, in fact it is BSON (binary of JSON). MongoDB is highly scalable and allows easy replication. Every entity can have whichever attributes it needs and skip whichever attributes it doesn’t needs. JavaScript is the interface language for MongoDB.

The next step is to install MongoDB on our system.