How to Get Started with MongoDB Database?

What is MongoDB?

Mongodb is a cross-platform, document oriented database that provides, high performance, high availability and easy scalability. It works on concept of collections and documents. If the load increases, by adding more nodes the performance can be retained. It can be used to store data for very high performance applications.

What is document oriented database?

Mongodb stores data as documents. So it is a document oriented database. A document is a set of key-value pairs. Documents have dynamic schema. Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection’s documents may hold different types of data.

Database:

Database is a physical container for collections. Each database gets its own set of files in the file system. A single server typically has multiple databases. A record in Mongodb is a document, which is a data structure composed of field and value pairs. Documents are similar to JSON objects. The values of fields may include other documents, arrays, and arrays of documents.

Collection:

Collection is a group of documents. It is the equivalent of an RDBMS table. A collection exists within a single database. Collections do not enforce a schema. Documents within a collection can have different fields. Typically, all documents in a collection are of similar or related purpose.

Below given table shows the relationship of RDBMS terminology with MongoDB

RDBMS MongoDB
Database Database
Table Collection
Row Document
Column Field
Primary key Primary key
MySQL server mongod
MySQL client mongo

Now it is time for us to get acquainted with some of the queries that we will be using later.

The following example shows the document structure of a blog site which is simply a comma separated key value pair.

{

_id: ObjectId(7df78ad8902c)

title: 'MongoDB Overview',

description: 'MongoDB is no sql database',

by: 'tutorials point',

url: 'http://www.tutorialspoint.com',

tags: ['mongodb', 'database', 'NoSQL'],

likes: 100,

comments: [

{

user:'user1',

message: 'My first comment',

dateCreated: new Date(2011,1,20,2,15),

like: 0

},

{

user:'user2',

message: 'My second comments',

dateCreated: new Date(2011,1,25,7,45),

like: 5

}

]

}

_id is a 12 bytes hexadecimal number which assures the uniqueness of every document. You can provide _id while inserting the document. MongoDB provides a unique id for every document. These 12 bytes’ first 4 bytes consist of current timestamp, next 3 bytes are for machine id, next 2 bytes are for process id of mongodb server and the remaining 3 bytes are simple incremental value.

Key Features:

High Performance:

It provides high performance data persistence. In particular,

  • Support for embedded data models reduces I/O activity on database system.
  • Indexes support faster queries and can include keys from embedded documents and arrays.

High Availability:

To provide high availability, Mongodb’s replication facility, called replica sets, provide:

  • automatic fail over.
  • data redundancy.

A replica set is a group of  servers that maintain the same data set, providing redundancy and increasing data availability.

Automatic Scaling:

  • It provides horizontal scalability as part of its core functionality.
  • Automatic shareing distributes data across a cluster of machines.
  • Replica sets can provide eventually-consistent reads for low-latency high throughput deployments.

Installation:

You can use mongodb in windows, Linux and also in OS X. But to use it in different OS we have to first learn the installation process. In different OS we are in need of different installation procedure. At first we will look at the process to install in Ubuntu, then windows and last but not the least OS X.

How to install in Ubuntu?

Import the public key used by the package management system.

The Ubuntu package management tools (i.e. dpkg and apt) ensure package consistency and authenticity by requiring that distributors sign packages with GPG keys. Issue the following command to import the public GPG Key:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10

Create a list file for Mongodb.

Create the /etc/apt/sources.list.d/mongodb.list list file using the following command:

echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list

Reload local package database.

Issue the following command to reload the local package database:

sudo apt-get update

Install the Mongodb packages.

You can install either the latest stable version or a specific version.

Install the latest stable version of Mongodb.

Issue the following command:

sudo apt-get install mongodb-org

Install a specific release of Mongodb.

Specify each component package individually and append the version number to the package name, as in the following example that installs the 2.6.1 release of MongoDB:

apt-get install mongodb-org=2.6.1 mongodb-org-server=2.6.1 mongodb-org-shell=2.6.1 mongodb-org-mongos=2.6.1 mongodb-org-tools=2.6.1

How to install mongodb in windows?

Determine which Mongodb build you need.

Mongodb for Windows Server 2008 R2 edition :

It runs only on Windows Server 2008 R2, Windows 7 64-bit, and newer versions of Windows. This build takes advantage of recent enhancements to the Windows Platform and cannot operate on older versions of Windows.

Mongodb for Windows 64-bit:

It  runs on any 64-bit version of Windows newer than Windows XP, including Windows Server 2008 R2 and Windows 7 64-bit.

Mongodb for Windows 32-bit :

It runs on any 32-bit version of Windows newer than Windows XP. 32-bit versions of MongoDB are only intended for older systems and for use in testing and development systems. 32-bit versions of MongoDB only support databases smaller than 2GB.

To find which version of Windows you are running, enter the following command in the Command Prompt:

wmic OS get osarchitecture

Download Mongodb for Windows:

Download the latest production release of Mongodb. Ensure you download the correct version of Mongodb for your Windows system. The 64-bit versions  does not work with 32-bit Windows.

Install the downloaded file:

In Windows Explorer, locate the downloaded MongoDB msi file, which typically is located in the default Downloads folder. Double-click the msi file. A set of screens will appear which will guide you through the installation process.

Mongodb is self-contained and does not have any other system dependencies. You can run MongoDB from any folder you choose. You may install MongoDB in any folder (e.g. D:\test\mongodb).

How to install mongodb in OS X?

Install Mongodb Manually

Only install  using this procedure if you cannot use homebrew.

Download the binary files for the desired release of MongoDB.

To download the latest release through the shell, issue the following:

curl -O http://downloads.mongodb.org/osx/mongodb-osx-x86_64-2.6.3.tgz

Extract the files from the downloaded archive.

For example, from a system shell, you can extract through the tar command:

tar -zxvf mongodb-osx-x86_64-2.6.3.tgz
  • Copy the extracted archive to the target directory.
  • Copy the extracted folder to the location from which Mongodb will run.
mkdir -p mongodb

cp -R -n mongodb-osx-x86_64-2.6.3/ mongodb

Ensure the location of the binaries is in the PATH variable.

The Mongodb binaries are in the bin/ directory of the archive. To ensure that the binaries are in your PATH, you can modify your PATH.

For example, you can add the following line to your shell’s rc file (e.g. ~/.bashrc):

export PATH=<mongodb-install-directory>:$PATH

In this article we have got to know, what mongodb is and how it is installed in different systems. Although this is only the first part of How to get started with mongodb? We have so many things to learn which we will discuss in the later articles.

Related Links:

1> How to Get Started with MongoDB?
2> How to Import and Export Through Mongodb?
3> How to Use Projection in MongoDB?
4> Using sort method in mongodb
5> Map-Reduce in MongoDB
6> Introduction to Replication in MongoDB
7> Deploying a Replica Set in MongoDB
8> Discussing Replication Lag in MongoDB
9> Replica Set Members in Mongodb
10> Working with Sharding in MongoDB
11> Working with Index in MongoDB
12> Working with Aggregation in MongoDB
13> How to Work with Aggregation Framework in MongoDB?
14> Working with Pipeline Concept in MongoDB
15> Discussing about Pipeline Expression in MongoDB

If you find this article helpful, you can connect us in Google+ and Twitter.

4 thoughts on “How to Get Started with MongoDB Database?

Leave a Reply

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