MongoDB is an open-source, document-oriented, NoSQL database that provides scalability, performance, and high availability. It is written in C++ and stores data in flexible, JSON-like documents, allowing for easy querying and retrieval of data. MongoDB is commonly used for applications that require scalability and large datasets, and provides features like auto-sharding, replication, and rich queries.
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.
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.
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
}
] }
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
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.
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.
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.