SlideShare a Scribd company logo
1 of 33
Download to read offline
An introduction to MongoDB
Raghvendra Parashar
raghvendra@gemsessence.com
MongoDB is a cross-platform, document oriented database that provides, high performance,
high availability, and easy scalability. MongoDB works on concept of collection and
document.
It is leading NoSQL database. MongoDB is written in c++.
Latest Production Release (3.0.0) — 3/3/2015
http://www.mongodb.org/downloads
NoSQL products (and among them MongoDB) should be used to meet challenges. If you have
one of the following challenges, you should consider MongoDB:
➔ You Expect a High Write Load
➔ You need High Availability in an Unreliable Environment
➔ You need to Grow Big (and Shard Your Data)
➔ Your Data is Location Based
➔ Your Data Set is Going to be Big (starting from 1GB) and Schema is Not Stable
http://java.dzone.com/articles/when-use-mongodb-rather-mysql
● Database
Database is a physical container for collections. Each database gets its own set of files on the
file system. A single MongoDB server typically has multiple databases.
● Collection
Collection is a group of MongoDB 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.
● Document
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.
Features
•Document-Oriented storage
•Full Index Support
•Replication & High Availability
•Auto-Sharding
•Querying
•Fast In-Place Updates
•GridFS
● Document Database
○ Documents (objects) map nicely to programming language data types.
○ Embedded documents and arrays reduce need for joins.
○ Dynamic schema makes polymorphism easier.
● High Performance
○ Embedding makes reads and writes fast.
○ Indexes can include keys from embedded documents and arrays.
○ Optional streaming writes (no acknowledgments).
● High Availability
○ Replicated servers with automatic master failover.
● Easy Scalability
○ Automatic sharding distributes collection data across machines.
○ Eventually-consistent reads can be distributed over replicated servers.
RDBMS MongoDB
Database Database
Table Collection
Tuple/Row Document
column Field
Table Join Embedded Documents
Primary Key Primary Key (Default
key _id provided by
mongodb itself)
Database Server and Client
Mysqld/Oracle mongod
mysql/sqlplus mongo
Below given table shows the relationship of RDBMS terminology with MongoDB
Below given 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. If you didn't provide then MongoDB provide a unique id
for every document. These 12 bytes first 4 bytes for the current timestamp, next 3 bytes for
machine id, next 2 bytes for process id of mongodb server and remaining 3 bytes are simple
incremental value.
Sample document
1. Document-Oriented storage
Example
Suppose a client needs a database design for his blog website and see
the differences between RDBMS and MongoDB schema design. Website
has the following requirements.
● Every post has the unique title, description and url.
● Every post can have one or more tags.
● Every post has the name of its publisher and total number of likes.
● Every Post have comments given by users along with their name,
message, data-time and likes.
● On each post there can be zero or more comments.
In RDBMS schema design for above requirements will have minimum
three tables.
So while showing the data, in RDBMS you need to join three tables and in mongodb
data will be shown from one collection only.
While in MongoDB schema design will have one collection post and has the following structure:
{
_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
}
]
}
The createCollection() Method
MongoDB db.createCollection(name, options) is used to create collection.
SYNTAX:
Basic syntax of createCollection() command is as follows
db.createCollection(name, options)
In the command, name is name of collection to be created. Options is a document and used
to specify configuration of collection
Parameter Type Description
Name String Name of the collection to be created
Options Document (Optional) Specify options about
memory size and indexing
Options parameter is optional, so you need to specify only name of the collection.
Following is the list of options you can use:
Field Type Description
capped Boolean (Optional) If true, enables a capped collection. Capped
collection is a collection fixed size collecction that
automatically overwrites its oldest entries when it reaches
its maximum size. If you specify true, you need to specify
size parameter also.
autoIndexI
D
Boolean (Optional) If true, automatically create index on _id field.s
Default value is false.
size number (Optional) Specifies a maximum size in bytes for a capped
collection. IfIf capped is true, then you need to specify
this field also.
max number (Optional) Specifies the maximum number of documents
allowed in the capped collection.
While inserting the document, MongoDB first checks size field of capped collection,
then it checks max field.
_id is 12 bytes hexadecimal number unique for every document in a collection. 12 bytes are
divided as follows:
_id: ObjectId(4 bytes timestamp, 3 bytes machine id, 2 bytes process id, 3 bytes incrementer
MongoDB supports many data-types whose list is given below:
● String : This is most commonly used datatype to store the data. String in
mongodb must be UTF-8 valid.
● Integer : This type is used to store a numerical value. Integer can be 32 bit or 64
bit depending upon your server.
● Boolean : This type is used to store a boolean (true/ false) value.
● Double : This type is used to store floating point values.
● Arrays : This type is used to store arrays or list or multiple values into one key.
● Timestamp : This can be handy for recording when a document has been
modified or added.
● Object : This datatype is used for embedded documents.
● Object ID : This datatype is used to store the document’s ID.
2. Full Index Support
Indexes support the efficient resolution of queries. Without indexes,
MongoDB must scan every document of a collection to select those
documents that match the query statement. This scan is highly inefficient
and require the mongod to process a large volume of data.
Indexes are special data structures, that store a small portion of the data
set in an easy to traverse form. The index stores the value of a specific
field or set of fields, ordered by the value of the field as specified in index.
The ensureIndex() Method
To create an index you need to use ensureIndex() method of mongodb.
SYNTAX:
Basic syntax of ensureIndex() method is as follows()
>db.COLLECTION_NAME.ensureIndex({KEY:1})
Here key is the name of field on which you want to create index and 1 is
for ascending order. To create index in descending order you need to use
-1.
EXAMPLE
>db.mycol.ensureIndex({"title":1})
>
In ensureIndex() method you can pass multiple fields, to create index on
multiple fields.
>db.mycol.ensureIndex({"title":1,"description":-1})
>
Parameter Type Description
background Boolean Builds the index in the background so that building an index does not block other database activities. Specify true
to build in the background. The default value is false.
unique Boolean Creates a unique index so that the collection will not accept insertion of documents where the index key or keys
match an existing value in the index. Specify true to create a unique index. The default value is false.
name string The name of the index. If unspecified, MongoDB generates an index name by concatenating the names of the
indexed fields and the sort order.
dropDups Boolean Creates a unique index on a field that may have duplicates. MongoDB indexes only the first occurrence of a key
and removes all documents from the collection that contain subsequent occurrences of that key. Specify true to
create unique index. The default value isfalse.
sparse Boolean If true, the index only references documents with the specified field. These indexes use less space but behave
differently in some situations (particularly sorts). The default value is false.
expireAfterSeconds integer Specifies a value, in seconds, as a TTL to control how long MongoDB retains documents in this collection.
v index
version
The index version number. The default index version depends on the version of mongod running when creating
the index.
weights document The weight is a number ranging from 1 to 99,999 and denotes the significance of the field relative to the other
indexed fields in terms of the score.
default_language string For a text index, the language that determines the list of stop words and the rules for the stemmer and tokenizer.
The default value is english.
language_override string For a text index, specify the name of the field in the document that contains, the language to override the default
language. The default value is language.
RDBMS Where Clause Equivalents in MongoDB
Operation Syntax Example RDBMS Equivalent
Equality {<key>:<value>} db.mycol.find({"by":"tutorials
point"}).pretty()
where by =
'tutorials point'
Less Than {<key>:{$lt:
<value>}}
db.mycol.find({"likes":{$lt:50}}).
pretty()
where likes < 50
Less Than
Equals
{<key>:{$lte:
<value>}}
db.mycol.find({"likes":{$lte:50}}).
pretty()
where likes <= 50
Greater Than {<key>:{$gt:
<value>}}
db.mycol.find({"likes":{$gt:50}}).
pretty()
where likes > 50
Greater Than
Equals
{<key>:{$gte:
<value>}}
db.mycol.find({"likes":{$gte:50}}).
pretty()
where likes >= 50
Not Equals {<key>:{$ne:
<value>}}
db.mycol.find({"likes":{$ne:50}}).
pretty()
where likes != 50
To query the document on the basis of some condition, you can use following operations
3. Replication & High Availability
Replication is the process of synchronizing data across multiple
servers. Replication provides redundancy and increases data
availability with multiple copies of data on different database
servers, replication protects a database from the loss of a single
server. Replication also allows you to recover from hardware failure
and service interruptions. With additional copies of the data, you
can dedicate one to disaster recovery, reporting, or backup.
Why Replication?
● To keep your data safe
● High (24*7) availability of data
● Disaster Recovery
● No downtime for maintenance (like backups, index rebuilds)
● Read scaling (extra copies to read from)
How replication works in MongoDB
MongoDB achieves replication by the use of replica set. A replica set is a group of
mongod instances that host the same data set. In a replica one node is primary node
that receives all write operations. All other instances, secondaries, apply operations
from the primary so that they have the same data set. Replica set can have only one
primary node.
1. Replica set is a group of two or more nodes (generally minimum 3 nodes are
required).
2. In a replica set one node is primary node and remaining nodes are secondary.
3. All data replicates from primary to secondary node.
4. At the time of automatic failover or maintenance, election establishes for primary
and a new primary node is elected.
5. After the recovery of failed node, it again join the replica set and works as a
secondary node.
A typical diagram of mongodb replication is shown in which client application always
interact with primary node and primary node then replicate the data to the secondary
nodes.
Replica set features
● A cluster of N nodes
● Anyone node can be primary
● All write operations goes to primary
● Automatic failover
● Automatic Recovery
● Consensus election of primary
Create MongoDB Replica set
Create 3 local MongoDB instances
● Make data directory
● mkdir -p /data/r0
mkdir -p /data/r1
mkdir -p /data/r2
● Start 3 local instances
● mongod --replSet cluster1 --port 27017 --dbpath /data/r0
mongod --replSet cluster1 --port 27018 --dbpath /data/r1
mongod --replSet cluster1 --port 27019 --dbpath /data/r2
● Add instance(s) to a cluster
● mongo myhost:27017
config = {_id: 'cluster1', members: [
{_id: 0, host: 'myhost1:27017'},
{_id: 1, host: 'myhost2:27018'},
{_id: 2, host: 'myhost3:27019'}]
}
rs.initiate(config);
cluster1:PRIMARY> rs.status()
ref : http://www.jonathanhui.com/mongodb-replication-replica-set
Vertical scaling Horizontal scaling
Sharding Introduction
Sharding is a method for storing data across multiple machines. MongoDB uses sharding to
support deployments with very large data sets and high throughput operations.
http://www.jonathanhui.com/mongodb-sharding
Purpose of Sharding
Database systems with large data sets and high throughput applications can
challenge the capacity of a single server. High query rates can exhaust the CPU
capacity of the server. Larger data sets exceed the storage capacity of a single
machine. Finally, working set sizes larger than the system’s RAM stress the I/O
capacity of disk drives.
To address these issues of scales, database systems have two basic approaches:
vertical scaling and sharding.
Vertical scaling adds more CPU and storage resources to increase capacity.
Scaling by adding capacity has limitations: high performance systems with large
numbers of CPUs and large amount of RAM are disproportionately more expensive
than smaller systems. Additionally, cloud-based providers may only allow users to
provision smaller instances. As a result there is a practical maximum capability for
vertical scaling.
Sharding, or horizontal scaling, by contrast, divides the data set and distributes the
data over multiple servers, or shards. Each shard is an independent database, and
collectively, the shards make up a single logical database.
Sharded cluster has the following components: shards, query routers and config servers.
Shards store the data. To provide high availability and data consistency, in a production
sharded cluster, each shard is a replica set.
Query Routers, or mongos instances, interface with client applications and direct operations
to the appropriate shard or shards. The query router processes and targets operations to
shards and then returns results to the clients. A sharded cluster can contain more than one
query router to divide the client request load. A client sends requests to one query router. Most
sharded clusters have many query routers.
Config servers store the cluster’s metadata. This data contains a mapping of the cluster’s
data set to the shards. The query router uses this metadata to target operations to specific
shards. Production sharded clusters have exactly 3 config servers.
GridFS
GridFS is a specification for storing and retrieving files that exceed the BSON-document size
limit of 16MB.
Instead of storing a file in a single document, GridFS divides a file into parts, or chunks, [1] and
stores each of those chunks as a separate document. By default GridFS limits chunk size to
255k. GridFS uses two collections to store files. One collection stores the file chunks, and the
other stores file metadata.
There are several courses runing by mongodb university, you can learn from here also
https://university.mongodb.com/courses/catalog
https://university.mongodb.com/courses/M102/about
You can try mongo queries here
http://try.mongodb.org/
Download:
http://www.mongodb.org/downloads
Refe:
http://www.mongodb.org/
http://www.jonathanhui.com/mongodb
http://www.tutorialspoint.com/mongodb/
Thank you for your attention!

More Related Content

What's hot (20)

Mongo db
Mongo dbMongo db
Mongo db
 
Mongodb
MongodbMongodb
Mongodb
 
Mongo db
Mongo dbMongo db
Mongo db
 
Top 10 frameworks of node js
Top 10 frameworks of node jsTop 10 frameworks of node js
Top 10 frameworks of node js
 
Mongo db workshop # 01
Mongo db workshop # 01Mongo db workshop # 01
Mongo db workshop # 01
 
Full metal mongo
Full metal mongoFull metal mongo
Full metal mongo
 
MongoDB
MongoDBMongoDB
MongoDB
 
MongoDB DOC v1.5
MongoDB DOC v1.5MongoDB DOC v1.5
MongoDB DOC v1.5
 
Introduction to mongo db
Introduction to mongo dbIntroduction to mongo db
Introduction to mongo db
 
Open source Technology
Open source TechnologyOpen source Technology
Open source Technology
 
Mongo db basics
Mongo db basicsMongo db basics
Mongo db basics
 
MongoDb - Details on the POC
MongoDb - Details on the POCMongoDb - Details on the POC
MongoDb - Details on the POC
 
MongoDB 2.4 and spring data
MongoDB 2.4 and spring dataMongoDB 2.4 and spring data
MongoDB 2.4 and spring data
 
MongoDB - An Introduction
MongoDB - An IntroductionMongoDB - An Introduction
MongoDB - An Introduction
 
Mongo db report
Mongo db reportMongo db report
Mongo db report
 
5 Pitfalls to Avoid with MongoDB
5 Pitfalls to Avoid with MongoDB5 Pitfalls to Avoid with MongoDB
5 Pitfalls to Avoid with MongoDB
 
Mongo db halloween party
Mongo db halloween partyMongo db halloween party
Mongo db halloween party
 
MongoDB NYC Python
MongoDB NYC PythonMongoDB NYC Python
MongoDB NYC Python
 
MongoDB for Beginners
MongoDB for BeginnersMongoDB for Beginners
MongoDB for Beginners
 
MongoDB basics & Introduction
MongoDB basics & IntroductionMongoDB basics & Introduction
MongoDB basics & Introduction
 

Viewers also liked

Typhoid fever
Typhoid feverTyphoid fever
Typhoid feverIra Dora
 
Introduction to NoSQL db and mongoDB
Introduction to NoSQL db and mongoDBIntroduction to NoSQL db and mongoDB
Introduction to NoSQL db and mongoDBbackslash451
 
Slash n: Technical Session 8 - Making Time - minute by minute - Janmejay Singh
Slash n: Technical Session 8 - Making Time - minute by minute - Janmejay SinghSlash n: Technical Session 8 - Making Time - minute by minute - Janmejay Singh
Slash n: Technical Session 8 - Making Time - minute by minute - Janmejay Singhslashn
 
MongoDB - A Document NoSQL Database
MongoDB - A Document NoSQL DatabaseMongoDB - A Document NoSQL Database
MongoDB - A Document NoSQL DatabaseRuben Inoto Soto
 
Mongo db – document oriented database
Mongo db – document oriented databaseMongo db – document oriented database
Mongo db – document oriented databaseWojciech Sznapka
 
MongoDB - javascript for your data
MongoDB - javascript for your dataMongoDB - javascript for your data
MongoDB - javascript for your dataaaronheckmann
 
Introduction to MongoDB Basics from SQL to NoSQL
Introduction to MongoDB Basics from SQL to NoSQLIntroduction to MongoDB Basics from SQL to NoSQL
Introduction to MongoDB Basics from SQL to NoSQLMayur Patil
 
Introduction to NoSQL with MongoDB
Introduction to NoSQL with MongoDBIntroduction to NoSQL with MongoDB
Introduction to NoSQL with MongoDBHector Correa
 
MongoDB NoSQL - Developer Guide
MongoDB NoSQL - Developer GuideMongoDB NoSQL - Developer Guide
MongoDB NoSQL - Developer GuideShiv K Sah
 
Mongodb intro
Mongodb introMongodb intro
Mongodb introchristkv
 
MongoDB: Intro & Application for Big Data
MongoDB: Intro & Application  for Big DataMongoDB: Intro & Application  for Big Data
MongoDB: Intro & Application for Big DataTakahiro Inoue
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDBJustin Smestad
 
Text databases and information retrieval
Text databases and information retrievalText databases and information retrieval
Text databases and information retrievalunyil96
 
MongoDB Introduction - Document Oriented Nosql Database
MongoDB Introduction - Document Oriented Nosql DatabaseMongoDB Introduction - Document Oriented Nosql Database
MongoDB Introduction - Document Oriented Nosql DatabaseSudhir Patil
 
OSCON 2012 MongoDB Tutorial
OSCON 2012 MongoDB TutorialOSCON 2012 MongoDB Tutorial
OSCON 2012 MongoDB TutorialSteven Francia
 
MongoDB vs Mysql. A devops point of view
MongoDB vs Mysql. A devops point of viewMongoDB vs Mysql. A devops point of view
MongoDB vs Mysql. A devops point of viewPierre Baillet
 

Viewers also liked (20)

Typhoid fever
Typhoid feverTyphoid fever
Typhoid fever
 
Introduction to NoSQL db and mongoDB
Introduction to NoSQL db and mongoDBIntroduction to NoSQL db and mongoDB
Introduction to NoSQL db and mongoDB
 
No SQL and MongoDB - Hyderabad Scalability Meetup
No SQL and MongoDB - Hyderabad Scalability MeetupNo SQL and MongoDB - Hyderabad Scalability Meetup
No SQL and MongoDB - Hyderabad Scalability Meetup
 
Slash n: Technical Session 8 - Making Time - minute by minute - Janmejay Singh
Slash n: Technical Session 8 - Making Time - minute by minute - Janmejay SinghSlash n: Technical Session 8 - Making Time - minute by minute - Janmejay Singh
Slash n: Technical Session 8 - Making Time - minute by minute - Janmejay Singh
 
MongoDB - A Document NoSQL Database
MongoDB - A Document NoSQL DatabaseMongoDB - A Document NoSQL Database
MongoDB - A Document NoSQL Database
 
Mongo db – document oriented database
Mongo db – document oriented databaseMongo db – document oriented database
Mongo db – document oriented database
 
MongoDB - javascript for your data
MongoDB - javascript for your dataMongoDB - javascript for your data
MongoDB - javascript for your data
 
Introduction to MongoDB Basics from SQL to NoSQL
Introduction to MongoDB Basics from SQL to NoSQLIntroduction to MongoDB Basics from SQL to NoSQL
Introduction to MongoDB Basics from SQL to NoSQL
 
Introduction to NoSQL with MongoDB
Introduction to NoSQL with MongoDBIntroduction to NoSQL with MongoDB
Introduction to NoSQL with MongoDB
 
MongoDB NoSQL - Developer Guide
MongoDB NoSQL - Developer GuideMongoDB NoSQL - Developer Guide
MongoDB NoSQL - Developer Guide
 
MongoDB
MongoDBMongoDB
MongoDB
 
Mongodb intro
Mongodb introMongodb intro
Mongodb intro
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
MongoDB: Intro & Application for Big Data
MongoDB: Intro & Application  for Big DataMongoDB: Intro & Application  for Big Data
MongoDB: Intro & Application for Big Data
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
A Brief MongoDB Intro
A Brief MongoDB IntroA Brief MongoDB Intro
A Brief MongoDB Intro
 
Text databases and information retrieval
Text databases and information retrievalText databases and information retrieval
Text databases and information retrieval
 
MongoDB Introduction - Document Oriented Nosql Database
MongoDB Introduction - Document Oriented Nosql DatabaseMongoDB Introduction - Document Oriented Nosql Database
MongoDB Introduction - Document Oriented Nosql Database
 
OSCON 2012 MongoDB Tutorial
OSCON 2012 MongoDB TutorialOSCON 2012 MongoDB Tutorial
OSCON 2012 MongoDB Tutorial
 
MongoDB vs Mysql. A devops point of view
MongoDB vs Mysql. A devops point of viewMongoDB vs Mysql. A devops point of view
MongoDB vs Mysql. A devops point of view
 

Similar to Mongodb Introduction

Top MongoDB interview Questions and Answers
Top MongoDB interview Questions and AnswersTop MongoDB interview Questions and Answers
Top MongoDB interview Questions and Answersjeetendra mandal
 
UNIT-1 MongoDB.pptx
UNIT-1 MongoDB.pptxUNIT-1 MongoDB.pptx
UNIT-1 MongoDB.pptxDharaDarji5
 
Basics of MongoDB
Basics of MongoDB Basics of MongoDB
Basics of MongoDB Habilelabs
 
Mongo db Quick Guide
Mongo db Quick GuideMongo db Quick Guide
Mongo db Quick GuideSourabh Sahu
 
3-Mongodb and Mapreduce Programming.pdf
3-Mongodb and Mapreduce Programming.pdf3-Mongodb and Mapreduce Programming.pdf
3-Mongodb and Mapreduce Programming.pdfMarianJRuben
 
Kalp Corporate MongoDB Tutorials
Kalp Corporate MongoDB TutorialsKalp Corporate MongoDB Tutorials
Kalp Corporate MongoDB TutorialsKalp Corporate
 
MongoDB NoSQL database a deep dive -MyWhitePaper
MongoDB  NoSQL database a deep dive -MyWhitePaperMongoDB  NoSQL database a deep dive -MyWhitePaper
MongoDB NoSQL database a deep dive -MyWhitePaperRajesh Kumar
 
MongoDB - An Introduction
MongoDB - An IntroductionMongoDB - An Introduction
MongoDB - An Introductiondinkar thakur
 
Introduction to MongoDB and its best practices
Introduction to MongoDB and its best practicesIntroduction to MongoDB and its best practices
Introduction to MongoDB and its best practicesAshishRathore72
 
Storage dei dati con MongoDB
Storage dei dati con MongoDBStorage dei dati con MongoDB
Storage dei dati con MongoDBAndrea Balducci
 
Node Js, AngularJs and Express Js Tutorial
Node Js, AngularJs and Express Js TutorialNode Js, AngularJs and Express Js Tutorial
Node Js, AngularJs and Express Js TutorialPHP Support
 
mongodb11 (1) (1).pptx
mongodb11 (1) (1).pptxmongodb11 (1) (1).pptx
mongodb11 (1) (1).pptxRoopaR36
 
NOSQL and MongoDB Database
NOSQL and MongoDB DatabaseNOSQL and MongoDB Database
NOSQL and MongoDB DatabaseTariqul islam
 
What are the major components of MongoDB and the major tools used in it.docx
What are the major components of MongoDB and the major tools used in it.docxWhat are the major components of MongoDB and the major tools used in it.docx
What are the major components of MongoDB and the major tools used in it.docxTechnogeeks
 
MongoDB World 2019: Fast Machine Learning Development with MongoDB
MongoDB World 2019: Fast Machine Learning Development with MongoDBMongoDB World 2019: Fast Machine Learning Development with MongoDB
MongoDB World 2019: Fast Machine Learning Development with MongoDBMongoDB
 

Similar to Mongodb Introduction (20)

Mongo db
Mongo dbMongo db
Mongo db
 
Mongodb By Vipin
Mongodb By VipinMongodb By Vipin
Mongodb By Vipin
 
Query Optimization in MongoDB
Query Optimization in MongoDBQuery Optimization in MongoDB
Query Optimization in MongoDB
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
Top MongoDB interview Questions and Answers
Top MongoDB interview Questions and AnswersTop MongoDB interview Questions and Answers
Top MongoDB interview Questions and Answers
 
UNIT-1 MongoDB.pptx
UNIT-1 MongoDB.pptxUNIT-1 MongoDB.pptx
UNIT-1 MongoDB.pptx
 
Basics of MongoDB
Basics of MongoDB Basics of MongoDB
Basics of MongoDB
 
Mongo db Quick Guide
Mongo db Quick GuideMongo db Quick Guide
Mongo db Quick Guide
 
3-Mongodb and Mapreduce Programming.pdf
3-Mongodb and Mapreduce Programming.pdf3-Mongodb and Mapreduce Programming.pdf
3-Mongodb and Mapreduce Programming.pdf
 
Mongo db dhruba
Mongo db dhrubaMongo db dhruba
Mongo db dhruba
 
Kalp Corporate MongoDB Tutorials
Kalp Corporate MongoDB TutorialsKalp Corporate MongoDB Tutorials
Kalp Corporate MongoDB Tutorials
 
MongoDB NoSQL database a deep dive -MyWhitePaper
MongoDB  NoSQL database a deep dive -MyWhitePaperMongoDB  NoSQL database a deep dive -MyWhitePaper
MongoDB NoSQL database a deep dive -MyWhitePaper
 
MongoDB - An Introduction
MongoDB - An IntroductionMongoDB - An Introduction
MongoDB - An Introduction
 
Introduction to MongoDB and its best practices
Introduction to MongoDB and its best practicesIntroduction to MongoDB and its best practices
Introduction to MongoDB and its best practices
 
Storage dei dati con MongoDB
Storage dei dati con MongoDBStorage dei dati con MongoDB
Storage dei dati con MongoDB
 
Node Js, AngularJs and Express Js Tutorial
Node Js, AngularJs and Express Js TutorialNode Js, AngularJs and Express Js Tutorial
Node Js, AngularJs and Express Js Tutorial
 
mongodb11 (1) (1).pptx
mongodb11 (1) (1).pptxmongodb11 (1) (1).pptx
mongodb11 (1) (1).pptx
 
NOSQL and MongoDB Database
NOSQL and MongoDB DatabaseNOSQL and MongoDB Database
NOSQL and MongoDB Database
 
What are the major components of MongoDB and the major tools used in it.docx
What are the major components of MongoDB and the major tools used in it.docxWhat are the major components of MongoDB and the major tools used in it.docx
What are the major components of MongoDB and the major tools used in it.docx
 
MongoDB World 2019: Fast Machine Learning Development with MongoDB
MongoDB World 2019: Fast Machine Learning Development with MongoDBMongoDB World 2019: Fast Machine Learning Development with MongoDB
MongoDB World 2019: Fast Machine Learning Development with MongoDB
 

Recently uploaded

Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfLivetecs LLC
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 

Recently uploaded (20)

Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdf
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 

Mongodb Introduction

  • 1. An introduction to MongoDB Raghvendra Parashar raghvendra@gemsessence.com
  • 2. MongoDB is a cross-platform, document oriented database that provides, high performance, high availability, and easy scalability. MongoDB works on concept of collection and document. It is leading NoSQL database. MongoDB is written in c++. Latest Production Release (3.0.0) — 3/3/2015 http://www.mongodb.org/downloads
  • 3. NoSQL products (and among them MongoDB) should be used to meet challenges. If you have one of the following challenges, you should consider MongoDB: ➔ You Expect a High Write Load ➔ You need High Availability in an Unreliable Environment ➔ You need to Grow Big (and Shard Your Data) ➔ Your Data is Location Based ➔ Your Data Set is Going to be Big (starting from 1GB) and Schema is Not Stable http://java.dzone.com/articles/when-use-mongodb-rather-mysql
  • 4. ● Database Database is a physical container for collections. Each database gets its own set of files on the file system. A single MongoDB server typically has multiple databases. ● Collection Collection is a group of MongoDB 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. ● Document 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.
  • 5. Features •Document-Oriented storage •Full Index Support •Replication & High Availability •Auto-Sharding •Querying •Fast In-Place Updates •GridFS
  • 6. ● Document Database ○ Documents (objects) map nicely to programming language data types. ○ Embedded documents and arrays reduce need for joins. ○ Dynamic schema makes polymorphism easier. ● High Performance ○ Embedding makes reads and writes fast. ○ Indexes can include keys from embedded documents and arrays. ○ Optional streaming writes (no acknowledgments). ● High Availability ○ Replicated servers with automatic master failover. ● Easy Scalability ○ Automatic sharding distributes collection data across machines. ○ Eventually-consistent reads can be distributed over replicated servers.
  • 7.
  • 8. RDBMS MongoDB Database Database Table Collection Tuple/Row Document column Field Table Join Embedded Documents Primary Key Primary Key (Default key _id provided by mongodb itself) Database Server and Client Mysqld/Oracle mongod mysql/sqlplus mongo Below given table shows the relationship of RDBMS terminology with MongoDB
  • 9. Below given 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. If you didn't provide then MongoDB provide a unique id for every document. These 12 bytes first 4 bytes for the current timestamp, next 3 bytes for machine id, next 2 bytes for process id of mongodb server and remaining 3 bytes are simple incremental value. Sample document
  • 10. 1. Document-Oriented storage Example Suppose a client needs a database design for his blog website and see the differences between RDBMS and MongoDB schema design. Website has the following requirements. ● Every post has the unique title, description and url. ● Every post can have one or more tags. ● Every post has the name of its publisher and total number of likes. ● Every Post have comments given by users along with their name, message, data-time and likes. ● On each post there can be zero or more comments. In RDBMS schema design for above requirements will have minimum three tables.
  • 11.
  • 12. So while showing the data, in RDBMS you need to join three tables and in mongodb data will be shown from one collection only. While in MongoDB schema design will have one collection post and has the following structure: { _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 } ] }
  • 13. The createCollection() Method MongoDB db.createCollection(name, options) is used to create collection. SYNTAX: Basic syntax of createCollection() command is as follows db.createCollection(name, options) In the command, name is name of collection to be created. Options is a document and used to specify configuration of collection Parameter Type Description Name String Name of the collection to be created Options Document (Optional) Specify options about memory size and indexing Options parameter is optional, so you need to specify only name of the collection.
  • 14. Following is the list of options you can use: Field Type Description capped Boolean (Optional) If true, enables a capped collection. Capped collection is a collection fixed size collecction that automatically overwrites its oldest entries when it reaches its maximum size. If you specify true, you need to specify size parameter also. autoIndexI D Boolean (Optional) If true, automatically create index on _id field.s Default value is false. size number (Optional) Specifies a maximum size in bytes for a capped collection. IfIf capped is true, then you need to specify this field also. max number (Optional) Specifies the maximum number of documents allowed in the capped collection. While inserting the document, MongoDB first checks size field of capped collection, then it checks max field.
  • 15. _id is 12 bytes hexadecimal number unique for every document in a collection. 12 bytes are divided as follows: _id: ObjectId(4 bytes timestamp, 3 bytes machine id, 2 bytes process id, 3 bytes incrementer MongoDB supports many data-types whose list is given below: ● String : This is most commonly used datatype to store the data. String in mongodb must be UTF-8 valid. ● Integer : This type is used to store a numerical value. Integer can be 32 bit or 64 bit depending upon your server. ● Boolean : This type is used to store a boolean (true/ false) value. ● Double : This type is used to store floating point values. ● Arrays : This type is used to store arrays or list or multiple values into one key. ● Timestamp : This can be handy for recording when a document has been modified or added. ● Object : This datatype is used for embedded documents. ● Object ID : This datatype is used to store the document’s ID.
  • 16. 2. Full Index Support Indexes support the efficient resolution of queries. Without indexes, MongoDB must scan every document of a collection to select those documents that match the query statement. This scan is highly inefficient and require the mongod to process a large volume of data. Indexes are special data structures, that store a small portion of the data set in an easy to traverse form. The index stores the value of a specific field or set of fields, ordered by the value of the field as specified in index. The ensureIndex() Method To create an index you need to use ensureIndex() method of mongodb.
  • 17. SYNTAX: Basic syntax of ensureIndex() method is as follows() >db.COLLECTION_NAME.ensureIndex({KEY:1}) Here key is the name of field on which you want to create index and 1 is for ascending order. To create index in descending order you need to use -1. EXAMPLE >db.mycol.ensureIndex({"title":1}) > In ensureIndex() method you can pass multiple fields, to create index on multiple fields. >db.mycol.ensureIndex({"title":1,"description":-1}) >
  • 18. Parameter Type Description background Boolean Builds the index in the background so that building an index does not block other database activities. Specify true to build in the background. The default value is false. unique Boolean Creates a unique index so that the collection will not accept insertion of documents where the index key or keys match an existing value in the index. Specify true to create a unique index. The default value is false. name string The name of the index. If unspecified, MongoDB generates an index name by concatenating the names of the indexed fields and the sort order. dropDups Boolean Creates a unique index on a field that may have duplicates. MongoDB indexes only the first occurrence of a key and removes all documents from the collection that contain subsequent occurrences of that key. Specify true to create unique index. The default value isfalse. sparse Boolean If true, the index only references documents with the specified field. These indexes use less space but behave differently in some situations (particularly sorts). The default value is false. expireAfterSeconds integer Specifies a value, in seconds, as a TTL to control how long MongoDB retains documents in this collection. v index version The index version number. The default index version depends on the version of mongod running when creating the index. weights document The weight is a number ranging from 1 to 99,999 and denotes the significance of the field relative to the other indexed fields in terms of the score. default_language string For a text index, the language that determines the list of stop words and the rules for the stemmer and tokenizer. The default value is english. language_override string For a text index, specify the name of the field in the document that contains, the language to override the default language. The default value is language.
  • 19. RDBMS Where Clause Equivalents in MongoDB Operation Syntax Example RDBMS Equivalent Equality {<key>:<value>} db.mycol.find({"by":"tutorials point"}).pretty() where by = 'tutorials point' Less Than {<key>:{$lt: <value>}} db.mycol.find({"likes":{$lt:50}}). pretty() where likes < 50 Less Than Equals {<key>:{$lte: <value>}} db.mycol.find({"likes":{$lte:50}}). pretty() where likes <= 50 Greater Than {<key>:{$gt: <value>}} db.mycol.find({"likes":{$gt:50}}). pretty() where likes > 50 Greater Than Equals {<key>:{$gte: <value>}} db.mycol.find({"likes":{$gte:50}}). pretty() where likes >= 50 Not Equals {<key>:{$ne: <value>}} db.mycol.find({"likes":{$ne:50}}). pretty() where likes != 50 To query the document on the basis of some condition, you can use following operations
  • 20. 3. Replication & High Availability Replication is the process of synchronizing data across multiple servers. Replication provides redundancy and increases data availability with multiple copies of data on different database servers, replication protects a database from the loss of a single server. Replication also allows you to recover from hardware failure and service interruptions. With additional copies of the data, you can dedicate one to disaster recovery, reporting, or backup. Why Replication? ● To keep your data safe ● High (24*7) availability of data ● Disaster Recovery ● No downtime for maintenance (like backups, index rebuilds) ● Read scaling (extra copies to read from)
  • 21. How replication works in MongoDB MongoDB achieves replication by the use of replica set. A replica set is a group of mongod instances that host the same data set. In a replica one node is primary node that receives all write operations. All other instances, secondaries, apply operations from the primary so that they have the same data set. Replica set can have only one primary node. 1. Replica set is a group of two or more nodes (generally minimum 3 nodes are required). 2. In a replica set one node is primary node and remaining nodes are secondary. 3. All data replicates from primary to secondary node. 4. At the time of automatic failover or maintenance, election establishes for primary and a new primary node is elected. 5. After the recovery of failed node, it again join the replica set and works as a secondary node. A typical diagram of mongodb replication is shown in which client application always interact with primary node and primary node then replicate the data to the secondary nodes.
  • 22.
  • 23. Replica set features ● A cluster of N nodes ● Anyone node can be primary ● All write operations goes to primary ● Automatic failover ● Automatic Recovery ● Consensus election of primary
  • 24. Create MongoDB Replica set Create 3 local MongoDB instances ● Make data directory ● mkdir -p /data/r0 mkdir -p /data/r1 mkdir -p /data/r2 ● Start 3 local instances ● mongod --replSet cluster1 --port 27017 --dbpath /data/r0 mongod --replSet cluster1 --port 27018 --dbpath /data/r1 mongod --replSet cluster1 --port 27019 --dbpath /data/r2
  • 25. ● Add instance(s) to a cluster ● mongo myhost:27017 config = {_id: 'cluster1', members: [ {_id: 0, host: 'myhost1:27017'}, {_id: 1, host: 'myhost2:27018'}, {_id: 2, host: 'myhost3:27019'}] } rs.initiate(config); cluster1:PRIMARY> rs.status() ref : http://www.jonathanhui.com/mongodb-replication-replica-set
  • 27. Sharding Introduction Sharding is a method for storing data across multiple machines. MongoDB uses sharding to support deployments with very large data sets and high throughput operations. http://www.jonathanhui.com/mongodb-sharding Purpose of Sharding Database systems with large data sets and high throughput applications can challenge the capacity of a single server. High query rates can exhaust the CPU capacity of the server. Larger data sets exceed the storage capacity of a single machine. Finally, working set sizes larger than the system’s RAM stress the I/O capacity of disk drives. To address these issues of scales, database systems have two basic approaches: vertical scaling and sharding. Vertical scaling adds more CPU and storage resources to increase capacity. Scaling by adding capacity has limitations: high performance systems with large numbers of CPUs and large amount of RAM are disproportionately more expensive than smaller systems. Additionally, cloud-based providers may only allow users to provision smaller instances. As a result there is a practical maximum capability for vertical scaling. Sharding, or horizontal scaling, by contrast, divides the data set and distributes the data over multiple servers, or shards. Each shard is an independent database, and collectively, the shards make up a single logical database.
  • 28.
  • 29.
  • 30. Sharded cluster has the following components: shards, query routers and config servers. Shards store the data. To provide high availability and data consistency, in a production sharded cluster, each shard is a replica set. Query Routers, or mongos instances, interface with client applications and direct operations to the appropriate shard or shards. The query router processes and targets operations to shards and then returns results to the clients. A sharded cluster can contain more than one query router to divide the client request load. A client sends requests to one query router. Most sharded clusters have many query routers. Config servers store the cluster’s metadata. This data contains a mapping of the cluster’s data set to the shards. The query router uses this metadata to target operations to specific shards. Production sharded clusters have exactly 3 config servers.
  • 31. GridFS GridFS is a specification for storing and retrieving files that exceed the BSON-document size limit of 16MB. Instead of storing a file in a single document, GridFS divides a file into parts, or chunks, [1] and stores each of those chunks as a separate document. By default GridFS limits chunk size to 255k. GridFS uses two collections to store files. One collection stores the file chunks, and the other stores file metadata.
  • 32. There are several courses runing by mongodb university, you can learn from here also https://university.mongodb.com/courses/catalog https://university.mongodb.com/courses/M102/about You can try mongo queries here http://try.mongodb.org/ Download: http://www.mongodb.org/downloads Refe: http://www.mongodb.org/ http://www.jonathanhui.com/mongodb http://www.tutorialspoint.com/mongodb/
  • 33. Thank you for your attention!