Mongo DB
Mongo DB
MongoDB is a NoSQL (Not only SQL) database that stores large volumes of data in the form
of documents. MongoDB removes the concept of "rows" of conventional and relational data
models by introducing "documents." This offers the developers the flexibility to work with
evolving data models.
In simple words, you can say that - Mongo DB is a document-oriented database. It is an
open-source product, developed and supported by a company named 10gen.
MongoDB is available under a General Public License for free, and it is also available under
a Commercial license from the manufacturer.
The manufacturing company 10gen has defined MongoDB as:
"MongoDB is a scalable, open source, high performance, document-oriented database." -
10gen. MongoDB was designed to work with commodity servers. Now it is used by the
company of all sizes, across all industries.
History of MongoDB
The initial development of MongoDB began in 2007 when the company was building a
platform as a service similar to window azure. Window azure is a cloud computing platform
and infrastructure, created by Microsoft, to build, deploy and manage applications and
service through a global network. MongoDB was developed by a NewYork based
organization named 10gen which is now known as MongoDB Inc. It was initially developed
as a PAAS (Platform as a Service). Later in 2009, it is introduced in the market as an open
source database server that was maintained and supported by MongoDB Inc.
The first ready production of MongoDB has been considered from version 1.4 which was
released in March 2010.
MongoDB2.4.9 was the latest and stable version which was released on January 10, 2014.
Purpose of building MongoDB
It may be a very genuine question - "what was the need of MongoDB although there were
many databases in action?"
There is a simple answer:
All modern applications require big data, fast feature development, and flexible deployment,
and the older database systems are not competent enough, so MongoDB was needed.
The primary purpose of building MongoDB is:
o Scalability
o Performance
o High Availability
o Scaling from single server deployments to large, complex multi-site architectures.
Key points of MongoDB
o Develop Faster
o Deploy Easier
o Scale Bigger
First of all, we should know what is a document-oriented database?
Example of a document-oriented database
MongoDB is a document-oriented database. It is a key feature of MongoDB. It offers
document-oriented storage. It is very simple you can program it easily.
MongoDB stores data as documents, so it is known as a document-oriented database.
1. FirstName = "John",
2. Address = "Detroit",
3. Spouse = [{Name: "Angela"}].
4. FirstName ="John",
5. Address = "Wick"
There are two different documents (separated by ".").
Storing data in this manner is called as document-oriented database.
Mongo DB falls into a class of databases that calls Document Oriented Databases. There is
also a broad category of databases known as No SQL Databases.
Features of MongoDB
These are some important features of MongoDB:
1. Support ad hoc queries
In MongoDB, you can search by field, range query and it also supports regular expression
searches.
2. Indexing
You can index any field in a document.
3. Replication
MongoDB supports Master-Slave replication.
A master can perform Reads and Writes and a Slave copies data from the master and can only
be used for reads or backup (not writes)
4. Duplication of data
MongoDB can run over multiple servers. The data is duplicated to keep the system up and
also keep its running condition in case of hardware failure.
5. Load balancing
It has an automatic load-balancing configuration because of data placed in shards.
6. Supports map-reduce and aggregation tools.
7. Uses JavaScript instead of Procedures.
8. It is a schema-less database written in C++.
9. Provides high performance.
10. Stores files of any size easily without complicating your stack.
11. Easy to administer in the case of failures.
12. It also supports:
JSON data model with dynamic schemas
Auto-sharding for horizontal scalability
Built-in replication for high availability
Now a day many companies use MongoDB to create new types of applications, and improve
performance and availability.
NoSQL Databases
We know that MongoDB is a NoSQL Database, so it is very necessary to know about
NoSQL Databases to understand MongoDB thoroughly.
What is NoSQL Database
Databases can be divided into 3 types:
1. RDBMS (Relational Database Management System)
2. OLAP (Online Analytical Processing)
3. NoSQL (recently developed database)
NoSQL Database
NoSQL Database is used to refer to a non-SQL or nonrelational database.
It provides a mechanism for the storage and retrieval of data other than the model of the
tabular relation used in relational databases. NoSQL database doesn't use tables for storing
data. It is generally used to store big data and real-time web applications.
History behind the creation of NoSQL Databases
In the early 1970, Flat File Systems are used. Data were stored in flat files and the biggest
problems with flat files are each company implement their own flat files and there are no
standards. It is very difficult to store data in the files, retrieve data from files because there is
no standard way to store data.
Then the relational database was created by E.F. Codd and these databases answered the
question of having no standard way to store data. But later relational database also get a
problem that it could not handle big data, due to this problem there was a need of database
which can handle every types of problems then NoSQL database was developed.
Advantages of NoSQL
o It supports query language.
o It provides fast performance.
o It provides horizontal scalability.
MongoDB advantages over RDBMS
In recent days, MongoDB is a new and popularly used database. It is a document-based,
nonrelational database provider.
Although it is 100 times faster than the traditional database it is early to say that it will
broadly replace the traditional RDBMS. But it may be very useful in terms to gain
performance and scalability.
A Relational database has a typical schema design that shows a number of tables and the
relationship between these tables, while in MongoDB there is no concept of relationship.
MongoDB Advantages
o MongoDB is schema-less. It is a document database in which one collection holds
different documents.
o There may be a difference between the number of fields, content, and size of the
document from one to another.
o Structure of a single object is clear in MongoDB.
o There are no complex joins in MongoDB.
o MongoDB provides the facility of deep query because it supports a powerful
dynamic query on documents.
o It is very easy to scale.
o It uses internal memory for storing working sets and this is the reason of its fast
access.
Distinctive features of MongoDB
o Easy to use
o Light Weight
o Extremely faster than RDBMS
Where MongoDB should be used
o Big and complex data
o Mobile and social infrastructure
o Content management and delivery
o User data management
o Data hub
Performance analysis of MongoDB and RDBMS
o In relational database (RDBMS) tables are using as storing elements, while in
MongoDB collection is used.
o In the RDBMS, we have multiple schema and in each schema we create tables to store
data while, MongoDB is a document oriented database in which data is written in
BSON format which is a JSON like format.
o MongoDB is almost 100 times faster than traditional database systems.
MongoDB Datatypes
Following is a list of usable data types in MongoDB.
Data Description
Types
String String is the most commonly used datatype. It is used to store data. A string must be
UTF 8 valid in mongodb.
Integer Integer is used to store the numeric value. It can be 32 bit or 64 bit depending on the
server you are using.
Boolean This datatype is used to store boolean values. It just shows YES/NO values.
Min/Max This datatype compare a value against the lowest and highest bson elements.
Keys
Arrays This datatype is used to store a list or multiple values into a single key.
Date This datatype stores the current date or time in unix time format. It makes you possible
to specify your own date time by creating object of date and pass the value of date,
month, year into it.
There is no create database command in MongoDB. Actually, MongoDB do not provide any
command to create database.
It may be look like a weird concept, if you are from traditional SQL background where you
need to create a database, table and insert values in the table manually.
Here, in MongoDB you don't need to create a database manually because MongoDB will
create it automatically when you save the value into the defined collection at first time.
You also don't need to mention what you want to create, it will be automatically created at
the time you save the value into the defined collection.
Here one thing is very remarkable that you can create collection manually by
"db.createCollection()" but not the database.
Syntax:
1. use DATABASE_NAME
If the database already exists, it will return the existing database.
Let' take an example to demonstrate how a database is created in MongoDB. In the following
example, we are going to create a database "javatpointdb".
1. >use javatpointdb
Swithched to db javatpointdb
1. >db
javatpointdb
1. >show dbs
local 0.078GB
Here, your created database "javatpointdb" is not present in the list, insert at least one
document into it to display database:
1. >db.movie.insert({"name":"javatpoint"})
WriteResult({ "nInserted": 1})
1. >show dbs
javatpointdb 0.078GB
local 0.078GB
Syntax:
1. db.dropDatabase()
This syntax will delete the selected database. In the case you have not selected any database,
it will delete default "test" database.
1. >show dbs
javatpointdb 0.078GB
local 0.078GB
If you want to delete the database "javatpointdb", use the dropDatabase() command as
follows:
1. >use javatpointdb
switched to the db javatpointdb
1. >db.dropDatabase()
{ "dropped": "javatpointdb", "ok": 1}
1. >show dbs
local 0.078GB