By
C . Enoch Joshua
What is MONGO DB?
No Sql
Database
Open Source Cross Platform
Document
Oriented
Written in c++
 All the modern applications require big data,
 Fast features development,
 Flexible deployment,
 Older database systems not enough competent,
 Auto - sharding
 Rich queries
 Document Oriented Storage − Data is stored in the form of JSON style
documents.
Where to Use Mongo DB?
Main purpose to build Mongo DB
 Scalability
 Performance
 High Availability
 Scaling from single server deployments to large, complex multi-site
architectures.
 Develop Faster
 Deploy Easier
History of Mongo DB
 The initial development of Mongo DB began in 2007 when the company was
building a platform as a service similar to window azure.
 Mongo DB was developed by a New York based organization named 10gen
 Later in 2009, it is introduced in the market as an open source database server
 The first ready production of Mongo DB 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
December 22, 2016.
Who uses Mongo DB
7
Installing Mongo DB on Windows
1.Download the Mongo DB binary archive:
2.Extract Mongo DB archive:
 Go ahead and extract archive. After extracting, you will get the directories
inside archive as follows
 here , bin directory contains the binaries in form of executables , such as
mongod.exe, mongo.exe, monogexport.exe etc.
3.Setup up configuration to start stop Mongo DB:
 For starting and stopping the Mongo DB server, we need only
the bin/mongod.exe, which is the daemon process executable for Mongo DB.
Using command line options:
 With use of these command line options, we configure mongo daemon process
 Use the following commands to start the server process.
 Change to bin directory.
 now type following command to start the mongod process.
 While starting, Windows firewall may block the process
 Click “Allow access“ to proceed. After successful execution of command ,
it will show logging info in standard console itself
1 cd I:Serversmongodbbin
1 mongod --dbpath I:Serversdata --port 27017
D/B RDBMS & Mongo DB
RDBMS MongoDB
Database Database
Table Collection
Tuple/Row Document
column Field
Table Join Embedded Documents
Primary Key Primary Key (Default key _id provided
by mongo db itself)
 Example:
 Suppose a client needs a database design for his blog/website and see the
differences between RDBMS and Mongo DB schema design.
 In RDBMS schema will have minimum three tables.
 While in Mongo DB schema,
design will have one
collection post and the
following structure.
 So while showing the data, in
RDBMS you need to join
three tables and in Mongo
DB, data will be shown from
one collection only.
{ _id: POST_ID
title: TITLE_OF_POST,
description: POST_DESCRIPTION,
by: POST_BY,
url: URL_OF_POST,
tags: [TAG1, TAG2, TAG3],
likes: TOTAL_LIKES,
comments: [
{
user:'COMMENT_BY',
message: TEXT,
dateCreated: DATE_TIME, l
ike: LIKES
},
{
user:'COMMENT_BY',
message: TEXT,
dateCreated: DATE_TIME,
like: LIKES
}
] }
Mongo DB – Data Types
Mongo DB - Create Database
The use Command:
 Mongo DB use DATABASE_NAME is used to create database. The command
will create a new database if it doesn't exist, otherwise it will return the existing
database.
Syntax:
Example:
 If you want to create a database with name <mydb> then,
use DATABASE_NAME
>use mydb
switched to db mydb
>db
mydb
Mongo DB - Drop Database
 MongoDB db.dropDatabase() command is used to drop a existing database.
Syntax:
Example:
First, check the list of available databases by using the command, show dbs.
db.dropDatabase()
>show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
>
If you want to delete new database <mydb>,then dropDatabase() command would
be as follows
Now check list of databases.
>use mydb
switched to db mydb
>db.dropDatabase()
>{ "dropped" : "mydb", "ok" : 1 }
>
>show dbs
local 0.78125GB
test 0.23012GB
>
Mongo DB - Create Collection
 Syntax:
 Examples:
db.createCollection(name, options)
>use test
switched to db test
>db.createCollection("mycollection")
{ "ok" : 1 }
>
>db.createCollection("mycol", { capped : true, autoIndexID : true, size :
6142800, max : 10000 } )
{ "ok" : 1 }
>
Mongo DB - Drop Collection
 Syntax:
 Example:
db.COLLECTION_NAME.drop()
>use mydb
switched to db mydb
>show collections
Mycol
mydb
>db.mycollection.drop()
true
>show collections
mycol
Replication
Multi Replication
 Data can be replicated to multiple places simultaneously
 Odd number of machines are always needed in a replica set
Single Replication
 If you want to have only one or odd number of secondary, you need to
setup an arbiter
 Arbiter will always be the arbiter and can only vote. But primary can be
primary also can step down as secondary.
Failover
 When primary fails, remaining machines vote for electing new
primary
Sharding
 A method for storing data across multiple machines
 Data is partitioned using Shard Keys
Sharded Cluster
 shard: Each shard contains a
subset of the sharded data.
Each shard can be deployed as
a replica set.
 mongos: The mongos acts as a
query router, providing an
interface between client
applications and the sharded
cluster.
 config servers: Config servers
store metadata and
configuration settings for the
cluster.
Data Partitioning: Range Based
 A range of Shard Keys stay in a chunk
28
Data Partitioning: Hash Based
 A hash function on Shard Keys decides the chunk
29
Optimizing Shards: Splitting
 In a shard, when size of a chunk increases, the chunk is divided into two
30
Optimizing Shards: Balancing
 When number of chunks in a shard increase, a few chunks are migrated to
other shard
31
Map-Reduce
 Map-reduce is a data processing paradigm for condensing large volumes of data
into useful aggregated results. For map-reduce operations, Mongo DB provides
the map Reduce database command.
 Mongo DB applies the map phase to each input document.
 The map function emits key-value pairs.
 For those keys that have multiple values, Mongo DB applies the reduce phase,
which collects and condenses the aggregated data.
 Mongo DB then stores the results in a collection.
MongoDB for Beginners

MongoDB for Beginners

  • 1.
  • 2.
    What is MONGODB? No Sql Database Open Source Cross Platform Document Oriented Written in c++
  • 3.
     All themodern applications require big data,  Fast features development,  Flexible deployment,  Older database systems not enough competent,  Auto - sharding  Rich queries  Document Oriented Storage − Data is stored in the form of JSON style documents.
  • 4.
    Where to UseMongo DB?
  • 5.
    Main purpose tobuild Mongo DB  Scalability  Performance  High Availability  Scaling from single server deployments to large, complex multi-site architectures.  Develop Faster  Deploy Easier
  • 6.
    History of MongoDB  The initial development of Mongo DB began in 2007 when the company was building a platform as a service similar to window azure.  Mongo DB was developed by a New York based organization named 10gen  Later in 2009, it is introduced in the market as an open source database server  The first ready production of Mongo DB 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 December 22, 2016.
  • 7.
  • 8.
    Installing Mongo DBon Windows 1.Download the Mongo DB binary archive:
  • 9.
    2.Extract Mongo DBarchive:  Go ahead and extract archive. After extracting, you will get the directories inside archive as follows  here , bin directory contains the binaries in form of executables , such as mongod.exe, mongo.exe, monogexport.exe etc.
  • 10.
    3.Setup up configurationto start stop Mongo DB:  For starting and stopping the Mongo DB server, we need only the bin/mongod.exe, which is the daemon process executable for Mongo DB. Using command line options:  With use of these command line options, we configure mongo daemon process
  • 11.
     Use thefollowing commands to start the server process.  Change to bin directory.  now type following command to start the mongod process.  While starting, Windows firewall may block the process  Click “Allow access“ to proceed. After successful execution of command , it will show logging info in standard console itself 1 cd I:Serversmongodbbin 1 mongod --dbpath I:Serversdata --port 27017
  • 13.
    D/B RDBMS &Mongo DB RDBMS MongoDB Database Database Table Collection Tuple/Row Document column Field Table Join Embedded Documents Primary Key Primary Key (Default key _id provided by mongo db itself)
  • 14.
     Example:  Supposea client needs a database design for his blog/website and see the differences between RDBMS and Mongo DB schema design.  In RDBMS schema will have minimum three tables.
  • 15.
     While inMongo DB schema, design will have one collection post and the following structure.  So while showing the data, in RDBMS you need to join three tables and in Mongo DB, data will be shown from one collection only. { _id: POST_ID title: TITLE_OF_POST, description: POST_DESCRIPTION, by: POST_BY, url: URL_OF_POST, tags: [TAG1, TAG2, TAG3], likes: TOTAL_LIKES, comments: [ { user:'COMMENT_BY', message: TEXT, dateCreated: DATE_TIME, l ike: LIKES }, { user:'COMMENT_BY', message: TEXT, dateCreated: DATE_TIME, like: LIKES } ] }
  • 16.
    Mongo DB –Data Types
  • 17.
    Mongo DB -Create Database The use Command:  Mongo DB use DATABASE_NAME is used to create database. The command will create a new database if it doesn't exist, otherwise it will return the existing database. Syntax: Example:  If you want to create a database with name <mydb> then, use DATABASE_NAME >use mydb switched to db mydb >db mydb
  • 18.
    Mongo DB -Drop Database  MongoDB db.dropDatabase() command is used to drop a existing database. Syntax: Example: First, check the list of available databases by using the command, show dbs. db.dropDatabase() >show dbs local 0.78125GB mydb 0.23012GB test 0.23012GB >
  • 19.
    If you wantto delete new database <mydb>,then dropDatabase() command would be as follows Now check list of databases. >use mydb switched to db mydb >db.dropDatabase() >{ "dropped" : "mydb", "ok" : 1 } > >show dbs local 0.78125GB test 0.23012GB >
  • 20.
    Mongo DB -Create Collection  Syntax:  Examples: db.createCollection(name, options) >use test switched to db test >db.createCollection("mycollection") { "ok" : 1 } > >db.createCollection("mycol", { capped : true, autoIndexID : true, size : 6142800, max : 10000 } ) { "ok" : 1 } >
  • 21.
    Mongo DB -Drop Collection  Syntax:  Example: db.COLLECTION_NAME.drop() >use mydb switched to db mydb >show collections Mycol mydb >db.mycollection.drop() true >show collections mycol
  • 22.
  • 23.
    Multi Replication  Datacan be replicated to multiple places simultaneously  Odd number of machines are always needed in a replica set
  • 24.
    Single Replication  Ifyou want to have only one or odd number of secondary, you need to setup an arbiter  Arbiter will always be the arbiter and can only vote. But primary can be primary also can step down as secondary.
  • 25.
    Failover  When primaryfails, remaining machines vote for electing new primary
  • 26.
    Sharding  A methodfor storing data across multiple machines  Data is partitioned using Shard Keys
  • 27.
    Sharded Cluster  shard:Each shard contains a subset of the sharded data. Each shard can be deployed as a replica set.  mongos: The mongos acts as a query router, providing an interface between client applications and the sharded cluster.  config servers: Config servers store metadata and configuration settings for the cluster.
  • 28.
    Data Partitioning: RangeBased  A range of Shard Keys stay in a chunk 28
  • 29.
    Data Partitioning: HashBased  A hash function on Shard Keys decides the chunk 29
  • 30.
    Optimizing Shards: Splitting In a shard, when size of a chunk increases, the chunk is divided into two 30
  • 31.
    Optimizing Shards: Balancing When number of chunks in a shard increase, a few chunks are migrated to other shard 31
  • 32.
    Map-Reduce  Map-reduce isa data processing paradigm for condensing large volumes of data into useful aggregated results. For map-reduce operations, Mongo DB provides the map Reduce database command.  Mongo DB applies the map phase to each input document.  The map function emits key-value pairs.  For those keys that have multiple values, Mongo DB applies the reduce phase, which collects and condenses the aggregated data.  Mongo DB then stores the results in a collection.