Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
MongoDB
Introduction to
Apurva Vyas Neha Bhardwaj
Shivam Mittal Mahendra Reddy
Document Database
One of the most popular ways of storing data is a
document data model, where each record and its
associated data is thought of as a “document”.
In a document database, such as MongoDB, everything
related to a database object is encapsulated together.
Why use MongoDB?
• SQL was invented in the 70’s to store data.
But the environment for data, as well as
programming, has changed since the development
of the SQL database:
•The emergence of cloud computing has brought
deployment and storage costs down dramatically.
• The need to store unstructured data, such as social media
posts and multimedia, has grown rapidly.
• Agile development methods mean that the database
schema needs to change rapidly as demands evolve
Strategic Priorities
Enabling New &
Enhancing Existing Apps
Better Customer Experience
Lower TCOFaster Time to Market
• MongoDB stores documents (or) objects.
• Now-a-days, everyone works with objects
(Python/Ruby/Java/etc.)
• And we need Databases to persist our objects. Then why not
store objects directly ?
• Embedded documents and arrays reduce need for joins. No
Joins and No-multi document transactions.
What is MongoDB ?
MongoDb is a Scalable , High-Performance
Open-source, Schema-Free , Document-orientated
database.
Scalable
Non-relational DBs scale more easily, especially horizontally.
High Performance
Focus on speed, performance, flexibility.
Not concerned with transactional stuff and relational
semantics
DBs should be an on-demand commodity, in a cloudlike
fashion
Schema-Free
Document Oriented Database
Store all information for an object in a single instance.
• No spanning tables for all information
• No more normalization
#Each stored object can be different.
• Not all stord documents need to contain same data.
#handles semi structured,unstructured data.
RDBMS to MONGODB
DATABSE DATABASE
TABLE COLLECTION
TUPLE/ROW DOCUMENT
COLOUMN FIELD
PRIMARY KEY PRIMARY KEY (DEFAULT KEY _ID
PROVIDED BY MONGODB ITSELF)
Hitting RDBMS Limits
Data Types
•Unstructured data
•Semi-structured data
•Polymorphic data
Volume of Data
•Petabytes of data
•Trillions of records
•Millions of queries per
second
Agile Development
•Iterative
•Short development
cycles
•New workloads
New Architectures
•Horizontal scaling
•Commodity servers
•Cloud computing
RDBMS
The Power of Dynamic Schema
MongoDB
{
_id : ObjectId("4c4ba5e5e8aabf3"),
employee_name: "Dunham, Justin",
department : "Marketing",
title : "Product Manager, Web",
report_up: "Neray, Graham",
pay_band: “C",
benefits : [
{ type : "Health",
plan : "PPO Plus" },
{ type : "Dental",
plan : "Standard" }
]
}
Features
• Built for Speed
• Rich Document based queries for Easy
readability.
• Full Index Support for High Performance.
• Replication and Failover for High
Availability.
• Auto Sharding for Easy Scalability.
• Map / Reduce for Aggregation.
• The queries are written in document format
for easy readability and understanding.
• A database index is a data structure that
improves the speed of data retrieval
operations
REPLICATION
Process of synchronizing data across
multiple servers.
Provides redundancy and increases data
availability with multiple copies of data on
different database servers.
Why Replication
• Allows you to recover from hardware failure and
service interruptions
• Read scaling (extra copies to read from).
• High (24*7) availability of data.
• No downtime for maintenance (like backups,
index rebuilds, compaction)
How Replication Works in
MongoDB
MongoDB achieves replication by the use of replica set.
Replica set is a group of two or more nodes: Primary and
Secondary
All data replicates from primary to secondary
Client application always interacts with primary,
replicating the data on secondary.
Mongodb
Sharding
Sharding is the process of storing data records
across multiple machines.
It is MongoDB's approach to meeting the demands
of data growth.
Sharding solves the problem of increased data size
with horizontal scaling.
Why Sharding over
Replication ?
1. Single replica set has limitation of 12 nodes.
2. Memory can't be large enough when active
dataset is big
3. Latency sensitive queries still go to master
4. Vertical scaling is too expensive
Mongodb
MAP-REDUCE
1. MongoDB applies the map phase to each input
document.
2. For multiple key values, MongoDB applies
the reduce phase, which collects and condenses
the aggregated data.
3. It is same as Group By clause in sql but much
more efficient.
RDBMS: Blogging Platform
JOIN 5 tables
MongoDB:
Denormalized to 2 BSON Documents
Higher Performance: Data Locality
Some Cool features
• Geo-spatial Indexes for Geo-spatial queries.
$near, $within_distance, Bound queries (circle, box)
• GridFS
Stores Large Binary Files.
• Map/Reduce
GROUP BY in SQL, map/reduce in MongoDB.
Some Companies using MongoDB in Production
Mongodb Installation
MongoDB is a document oriented database.
Which provides
High performance,
High availability
Easy scalability.
MongoDB works on concept of collection and document.
Installation Process
Install MongoDB on Windows
Install MongoDB on Windows from
https://www.mongodb.org/downloads
(or) https://www.mongodb.com.
Mongodb
• check our system type whethere it is 64-bit or 32-bit
• choose setup Type complete then press Next
• Now Press Install
• Now Press Finish
• Now go to my computer.
• Copy the bin Location.
Mongodb
Mongodb
• Now go to drive C: there you add one folder name it as data.
Inside data again add another folder db.
Now click on ok.. ok.. ok..
Mongodb successfully installed.
open Command Prompt(cmd).
Type mongod
open another Command Prompt
Type mongo
Mongodb
• connecting to Mongodb
• To Run Mongodb directly by using Intelli Idea. Just type Intelli
idea on Google
• Download Ultimate.
Mongodb
Mongodb
Mongodb
Mongodb
Mongodb
COMMANDS
The db Command
To check your currently selected database, use the
command db
SYNTAX
db
EXAMPLE:
db
The USE Command
To create database in MONGODB we use this command
Syntax:
Use Database_name
The SHOW Command
SYNTAX
show dbs
EXAMPLE:
>show dbs
local 0.78125 GB
test 0.23012 GB
We created database (Movies) is not
present in list.
To display database, we need to insert at
least one document into it.
So to insert something in database we
have to use insert command
Example
db.movies.insert({”name”:”dead pool”})
WriteResult({ "nInserted" : 1 })
Show dbs
cinema 0.000GB
local 0.000GB
test 0.000GB
Drop database collection
Syntax
db.dropDatabase
Example
Use cinema
db.dropDatabase
Show dbs
The Create collection method
Syntax
db.createCollection(name, options)
“name” is name of collcetion and
“options” is a document and is used to
specify configuration of collection.
(“options” parameter is optional so we
need only to give name)
Example
Use football
db.createCollection(“players”)
To show collections use command
Show collections
The drop() Method
Syntax
db.collectionName.drop()
Example
db.players.drop()
To insert in collection
Syntax
db.COLLECTION_NAME.insert(document)
Example
db.players.insert({
Name:'Cristiano Ronaldo',
country:'Portugal',
club: 'Real Madrid',
Height: 1.85,
DOB:5/02/1985
})
The find and pretty method
SYNTAX
db.collection_name.find()
db.collection_name.find().pretty()
EXAMPLE
db.players.find()
db.players.find().pretty()
AND/OR in MongogDB
SYNTAX
db.mycol.find(
{
$and: [
{key1: value}, {key2: value2}
]}).pretty()
Example
db.players.find({$and:[{“country”: ”Spain”},{“club”:
“Real Madrid”}]})
OR command
db.players.find({$or:[{“country”: ”Spain”},{“club”:
“Real Madrid”}]})
The query of documents
depends on some condition
Operation syntax example RDBMS
equivalence
Equality {<key>:<value>} Db.players.find({“c
ountry”:”Brazil”}).
pretty()
Where
country=‘Brazil
Less than {<key>:{$lt:<value
>}}
Db.players.find({“
Height”:{$lt:1.8}}.p
retty()
Where Height<1.9
Greater than {<key>:{$gt:<value
>}}
Db.players.find({“
Height”:{$gt:1.8}}.p
retty()
Where Height>1.8
Not equal {<key>:{$ne:<value
>}}
Db.players.find({“c
ountry”:{$ne:”Brazi
l”}}
Where
Country!=Brazil
Example
db.players.find({“height”{$gt:1.8}})
db.players.find({“height”{$lt:1.8}})
MongoDB update method
SYNTAX
db.COLLECTION_NAME.update(selection criteria,update
value)
EXAMPLE
db.players.update({'Name':'Sergio
Ramos'},{$set:{'Name':'cavini'}})
If you want to update in multi documents then we can use
this command:
db.players.update({'Name':'Sergio
Ramos'},{$set:{'Name':'cavini'}}) ,{multi:true})
MongoDb save method
Syntax
db.COLLECTION_NAME.save({_id:ObjectId(),NEW
_DATA})
Eaxmple
db.mycol.save(
{
"_id" : ObjectId(5983548781331adf45ec7),
“Name":“Cristiano Ronaldo",
“Goals": 100 }
Remove method
SYNTAX
db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)
Example
db.players.remove({"Name": "Cristiano Ronaldo"})
If you want to delete whole document:
db.players.remove()
Different use command “find”
SYNTAX
db.COLLECTION_NAME.find({},{KEY:1})
EXAMPLE
db.players.find({},{_id:0,"Name":1})
Thr limit method
SYNTAX
db.COLLECTION_NAME.find().limit(NUMBER)
EXAMPLE
db.players.find({},{_id:0,"Name":1}).limit(5)
MongoDB Skip() Method
SYNTAX
db.COLLECTION_NAME.find().limit(NUMBER).skip(NUM
BER)
EXAMPLE
db.players.find({},{_id:0,"Name":1}).limit(5).skip(3)
THANK YOU

More Related Content

Mongodb