mongodb for beginners

33
By C . Enoch Joshua

Upload: enoch-joshua

Post on 09-Apr-2017

77 views

Category:

Software


1 download

TRANSCRIPT

Page 1: MongoDB for Beginners

By C . Enoch Joshua

Page 2: MongoDB for Beginners

What is MONGO DB?

No Sql Database

Open Source

Cross Platform

Document Oriented

Written in c++

Page 3: MongoDB for Beginners

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.

Page 4: MongoDB for Beginners

Where to Use Mongo DB?

Whe

re to

use

mon

go

db ?

Big Data

User Data Management

Mobile and Social Infrastructure

Content Management and

Delivery

Data Hub

Page 5: MongoDB for Beginners

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

Page 6: MongoDB for Beginners

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.

Page 7: MongoDB for Beginners

7

Who uses Mongo DB

Page 8: MongoDB for Beginners

Installing Mongo DB on Windows

1.Download the Mongo DB binary archive:

Page 9: MongoDB for Beginners

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.

Page 10: MongoDB for Beginners

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

Page 11: MongoDB for Beginners

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:\Servers\mongodb\bin

1 mongod --dbpath I:\Servers\data --port 27017

Page 12: MongoDB for Beginners
Page 13: MongoDB for Beginners

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)

Page 14: MongoDB for Beginners

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.

Page 15: MongoDB for Beginners

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, like: LIKES }, { user:'COMMENT_BY', message: TEXT, dateCreated: DATE_TIME, like: LIKES } ] }

Page 16: MongoDB for Beginners

Mongo DB – Data Types

Data Types

String

Integer

BooleanMin/Max

keysArray

Timestamp

Object

Null

Date

Page 17: MongoDB for Beginners

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

Page 18: MongoDB for Beginners

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 >

Page 19: MongoDB for Beginners

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 >

Page 20: MongoDB for Beginners

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 } >

Page 21: MongoDB for Beginners

Mongo DB - Drop Collection

Syntax:

Example:db.COLLECTION_NAME.drop()

>use mydb switched to db mydb >show collections Mycolmydb

>db.mycollection.drop() true

>show collections mycol

Page 22: MongoDB for Beginners

Replication

Page 23: MongoDB for Beginners

Multi Replication

Data can be replicated to multiple places simultaneously

Odd number of machines are always needed in a replica set

Page 24: MongoDB for Beginners

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.

Page 25: MongoDB for Beginners

Failover

When primary fails, remaining machines vote for electing new primary

Page 26: MongoDB for Beginners

Sharding

A method for storing data across multiple machines Data is partitioned using Shard Keys

Page 27: MongoDB for Beginners

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.

Page 28: MongoDB for Beginners

Data Partitioning: Range Based

A range of Shard Keys stay in a chunk

28

Page 29: MongoDB for Beginners

29

Data Partitioning: Hash Based A hash function on Shard Keys decides the chunk

Page 30: MongoDB for Beginners

30

Optimizing Shards: Splitting

In a shard, when size of a chunk increases, the chunk is divided into two

Page 31: MongoDB for Beginners

31

Optimizing Shards: Balancing

When number of chunks in a shard increase, a few chunks are migrated to other shard

Page 32: MongoDB for Beginners

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.

Page 33: MongoDB for Beginners