SlideShare ist ein Scribd-Unternehmen logo
1 von 41
Get Expertise with 
MongoDB 
Design Pattern
Agenda 
★ MongoDB Recap 
★ How mongoDB works? 
★ The _id 
★ In n Out of Query Execution. 
★ Indexes 
★ What is Replication? 
★ What is Sharding ?
About Us 
Amit Thakkar 
Tech Blogger @ 
CodeChutney.in 
JavaScript Lover 
Working on MEAN Stack 
Twitter: @amit_thakkar01 
LinkedIn: linkedin.com/in/amitthakkar01 
Facebook: facebook.com/amit.thakkar01 
Vibhor Kukreja 
JavaScript Ninja 
Working on MEAN Stack 
Twitter: @VibhorKukreja 
Email : vibhor.kukreja@intelligrape.com
MongoDB Recap 
1. No-Sql Database. 
2. Installation. 
3. Basic CRUD operation. 
4. Stores data in JSON. 
5. Schemaless. 
6. Great Performance. 
7. No Joins. 
8. Easily scalable.
MongoDB Recap
How mongoDB works? 
There is a process “mongod” that acts as a database server. 
This process attaches itself to a directory with the --dbpath option. The default 
dbpath is “/data/db”. 
And start listening to a specific port number via --port option.The default port is 
27017. 
> mongod --dbpath ~/testDB --port 28080 
You just need to create the testDB directory before allocating it to the 
mongod process.
Look what’s inside the created dbpath ? 
- /journal 
- test.ns // namespace for database 
- test.0 // raw data storage files 
- test.1 // raw data storage files 
But what is this journal directory for?
Journal Option 
Journal option does a write ahead logging to an ondisk 
journal to guarantee write operation. i.e write in memory 
and on-disk journal before in the data file. ( Data 
Consistency ) 
Where can we set this option? 
There is an configuration file for the mongod process. 
> cat /etc/mongodb.conf
How to get connected with mongod? 
There are two modes to interact with the DB. 
1. mongoDB Driver/Plugin 
- Node.js (mongoose) 
- Grails (GORM) 
- Python (pymongo) 
2. mongo Shell - supports javascript <3
mongo Shell 
To connect a mongo shell process with our mongod process, we just have to 
specify the port number on which the mongod process is listening. 
> mongo --port 28080 
To check the status of our mongo server, for a proper analysis we must start 
the mongostat process to check the server request/response. 
> mongostat --port 28080
Yeah !! Our Single instance Server is 
Up and Running. 
At this point of time , we have just created a single 
mongoDB server along with a mongo shell that 
interacts with this mongoDB server.
The _id 
It can be represented as a primary key in an collection. 
The mongo _id have few properties like, 
It must be unique. 
It can store any type of data, except boolean and array. 
eg - _id : “intelligrape” 
_id : { “company” : “intelligrape” } 
_id : 17 
_id : true //You can do this, but in this case you can have only two records in this collection 
By default it stores ObjectId(“HexString”), and it gives a unique value every time as it is computed with 
the help of the following. 
● The first 4 bytes representing the seconds 
● The next 3 bytes are the machine identifier 
● The next 2 bytes consists of process id 
● The last 3 bytes are a random counter value
_id impact on save() and insert() Query 
save( ) - is risky :P 
If there is an particular insert key already present in the collection, then it will 
replace that old document completely from the collection with the new one. 
( without even prompting it :D ) 
So, it may become risky to use save query in our Applications. Its better to use 
the insert() query as it throws “duplicate key error index” error.
Importing a document in mongoDB 
We can easily import a document in mongoDB. Mongo itself provides a process to 
import document. 
> mongoimport --db test --port 28080 --type json --collection person < testColl.json 
--db defines the database name 
--port server port number 
--type document type, which is to be import 
--collection name of the collection, where insertion is to be done
PowerOf2Sizes ( v2.6 +) 
What is move and padding in mongoDB storage? 
> db.runCommand({ collMod : ”collection_Name”,usePowerOf2Sizes : true })
In n Out of Query Execution. 
> db.person.find({}).sort({name:1}).skip(2).limit(3); 
> db.person.find({}).skip(2).sort({name:1}).limit(3); 
> db.person.find({}).limit(3).skip(2).sort({name:1}); 
What impact does they will make, on the result?
Bulk Write Operations ( v2.6+) 
Type of bulk operations - 
● Ordered 
● Unordered 
> var bulk = db.person.initializeUnorderedBulkOp(); 
> bulk.insert( { name : ”Joe” } ); 
> bulk.insert( { name : ”Tim” } ); 
> bulk.insert( { name : ”Steve” } ); 
> bulk.execute();
What is an Index? 
A data structure that can be used to make certain 
queries more efficient.
How can we index ? 
An index on _id is automatic. For more, ensureIndex : 
> db.posts.ensureIndex({“name”:1}); 
> db.posts.ensureIndex({name: 1, date: ‐1}); 
> db.posts.ensureIndex({title: 1},{unique: true, sparse:true,dropDups:true}); 
> db.posts.ensureIndex(...,{background: true}); 
> db.posts.ensureIndex({“comments.author”: 1}); 
{“tags”: [“mongodb”, “indexing”], ...} 
> db.posts.ensureIndex({“tags”: 1}); // multikey 
> db.posts.ensureIndex({“location”: “2d”}); //geospatial 
db.post.find({title:”mongo”}).hint({name:1,title:1});
What is Replication? 
● Replication is the process of synchronizing data across 
multiple servers. 
● Mongo achieves Replication through Replica Sets 
● Replica sets are a form of asynchronous master/slave 
replication 
● A replica set consists of two or more nodes that are 
copies of each other. (i.e.: replicas)
Purpose of Replication 
● Automated Failover / High Availability 
If primary fails then replica set will 
attempt to select another member 
to become the new primary. 
Use heartbeat signal to detect failure
● Distributed Read Load/Read Scaling 
By default, the primary node of a replica set is accessed for 
all reads and writes. 
● Disaster Recovery 
So, you have a pool of servers with one primary (the 
master) and N secondaries (slaves). If the primarycrashes 
or disappears, the other servers will hold an election to 
choose a new primary. Arbiter helps to achieve >50 votes 
in case of draw. 
connectionString = (“mongodb://localhost:37017", replicaSet="s0",w=4, 
j=True)
Lets Create a Replica Set :P 
mkdir -p ~/tempDB/rs1 ~/tempDB/rs2 ~/tempDB/rs3 
mongod --replSet m101 --logpath "1.log" --dbpath ~/tempDB/rs1 --port 
47017 --fork 
mongod --replSet m101 --logpath "2.log" --dbpath ~/tempDB/rs2 --port 
47018 --fork 
mongod --replSet m101 --logpath "3.log" --dbpath ~/tempDB/rs3 --port 
47019 --fork 
Seperate mongod processes have been created, lets connect them with 
each other.
Configuring Replica Sets. 
> mongo --port 47017 
config = { _id: "m101", members:[ 
{ _id : 0, host : "localhost:47017"}, 
{ _id : 1, host : "localhost:47018"}, 
{ _id : 2, host : "localhost:47019"} ] 
}; 
rs.initiate(config); 
rs.status();
What is Sharding ?
Lets Create Shards :P 
Shard 0 
mkdir -p ~/testDB/shard0/rs0 ~/testDB/shard0/rs1 ~/testDB/shard0/rs2 
mongod --replSet s0 --logpath "testDB/s0-r0.log" --dbpath ~/testDB/shard0/rs0 --port 37017 --fork -- 
shardsvr 
mongod --replSet s0 --logpath "testDB/s0-r1.log" --dbpath ~/testDB/shard0/rs1 --port 37018 --fork -- 
shardsvr 
mongod --replSet s0 --logpath "testDB/s0-r2.log" --dbpath ~/testDB/shard0/rs2 --port 37019 --fork -- 
shardsvr 
mongo --port 37017 
config = { _id: "s0", members:[ 
{ _id : 0, host : "localhost:37017" }, 
{ _id : 1, host : "localhost:37018" }, 
{ _id : 2, host : "localhost:37019" }]}; 
rs.initiate(config);
Shard 1 
mkdir -p ~/testDB/shard1/rs0 ~/testDB/shard1/rs1 ~/testDB/shard1/rs2 
mongod --replSet s1 --logpath "testDB/s1-r0.log" --dbpath ~/testDB/shard1/rs0 --port 47017 --fork -- 
shardsvr 
mongod --replSet s1 --logpath "testDB/s1-r1.log" --dbpath ~/testDB/shard1/rs1 --port 47018 --fork -- 
shardsvr 
mongod --replSet s1 --logpath "testDB/s1-r2.log" --dbpath ~/testDB/shard1/rs2 --port 47019 --fork -- 
shardsvr 
mongo --port 47017 
config = { _id: "s1", members:[ 
{ _id : 0, host : "localhost:47017" }, 
{ _id : 1, host : "localhost:47018" }, 
{ _id : 2, host : "localhost:47019" }]}; 
rs.initiate(config);
Shard 2 
mkdir -p ~/testDB/shard2/rs0 ~/testDB/shard2/rs1 ~/testDB/shard2/rs2 
mongod --replSet s2 --logpath "testDB/s2-r0.log" --dbpath ~/testDB/shard2/rs0 --port 57017 --fork -- 
shardsvr 
mongod --replSet s2 --logpath "testDB/s2-r1.log" --dbpath ~/testDB/shard2/rs1 --port 57018 --fork -- 
shardsvr 
mongod --replSet s2 --logpath "testDB/s2-r2.log" --dbpath ~/testDB/shard2/rs2 --port 57019 --fork -- 
shardsvr 
mongo --port 57017 
config = { _id: "s2", members:[ 
{ _id : 0, host : "localhost:57017" }, 
{ _id : 1, host : "localhost:57018" }, 
{ _id : 2, host : "localhost:57019" }]}; 
rs.initiate(config);
Congif Servers 
mkdir -p ~/testDB/config/config-a ~/testDB/config/config-b ~/testDB/config/config-c 
mongod --logpath "testDB/cfg-a.log" --dbpath ~/testDB/config/config-a --port 57040 - 
-fork --configsvr 
mongod --logpath "testDB/cfg-b.log" --dbpath ~/testDB/config/config-b --port 57041 - 
-fork --configsvr 
mongod --logpath "testDB/cfg-c.log" --dbpath ~/testDB/config/config-c --port 57042 - 
-fork --configsvr
Setting Up “mongos” 
mongos --logpath "testDB/mongos-1.log" --port 28888 --configdb 
localhost:57040,localhost:57041,localhost:57042 --fork 
mongo --port 28888 
db.adminCommand( { addshard : "s0/"+"localhost:37017" } ); 
db.adminCommand( { addshard : "s1/"+"localhost:47017" } ); 
db.adminCommand( { addshard : "s2/"+"localhost:57017" } ); 
db.adminCommand({enableSharding: "test"}) // test is the db name 
db.adminCommand({shardCollection: "test.grades", key: {month:1,student_id:1}});
Exercise Time
Congo !! Your Shard 
environment is up 
and running :)
Question Answer??
Get expertise with mongo db

Weitere ähnliche Inhalte

Was ist angesagt?

Mongodb introduction and_internal(simple)
Mongodb introduction and_internal(simple)Mongodb introduction and_internal(simple)
Mongodb introduction and_internal(simple)
Kai Zhao
 
Mongo db – document oriented database
Mongo db – document oriented databaseMongo db – document oriented database
Mongo db – document oriented database
Wojciech Sznapka
 

Was ist angesagt? (20)

Mongo DB 102
Mongo DB 102Mongo DB 102
Mongo DB 102
 
Basics of MongoDB
Basics of MongoDB Basics of MongoDB
Basics of MongoDB
 
MongoDB
MongoDBMongoDB
MongoDB
 
Mongodb
MongodbMongodb
Mongodb
 
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorialsMongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
 
MongoDB 101
MongoDB 101MongoDB 101
MongoDB 101
 
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
 
Intro to mongo db
Intro to mongo dbIntro to mongo db
Intro to mongo db
 
Mongo DB Presentation
Mongo DB PresentationMongo DB Presentation
Mongo DB Presentation
 
Mongodb introduction and_internal(simple)
Mongodb introduction and_internal(simple)Mongodb introduction and_internal(simple)
Mongodb introduction and_internal(simple)
 
The Basics of MongoDB
The Basics of MongoDBThe Basics of MongoDB
The Basics of MongoDB
 
Mongo DB
Mongo DBMongo DB
Mongo DB
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
An introduction to MongoDB
An introduction to MongoDBAn introduction to MongoDB
An introduction to MongoDB
 
Introduction to mongo db
Introduction to mongo dbIntroduction to mongo db
Introduction to mongo db
 
Mongo db dhruba
Mongo db dhrubaMongo db dhruba
Mongo db dhruba
 
MongodB Internals
MongodB InternalsMongodB Internals
MongodB Internals
 
Mongo db – document oriented database
Mongo db – document oriented databaseMongo db – document oriented database
Mongo db – document oriented database
 
MongoDB
MongoDBMongoDB
MongoDB
 

Andere mochten auch

Mongo sf easy java persistence
Mongo sf   easy java persistenceMongo sf   easy java persistence
Mongo sf easy java persistence
Scott Hernandez
 
Introducing Hibernate OGM: porting JPA applications to NoSQL, Sanne Grinovero...
Introducing Hibernate OGM: porting JPA applications to NoSQL, Sanne Grinovero...Introducing Hibernate OGM: porting JPA applications to NoSQL, Sanne Grinovero...
Introducing Hibernate OGM: porting JPA applications to NoSQL, Sanne Grinovero...
OpenBlend society
 
MongoDB- Crud Operation
MongoDB- Crud OperationMongoDB- Crud Operation
MongoDB- Crud Operation
Edureka!
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
Edureka!
 
Introduction to column oriented databases
Introduction to column oriented databasesIntroduction to column oriented databases
Introduction to column oriented databases
ArangoDB Database
 

Andere mochten auch (20)

Mongo DB
Mongo DBMongo DB
Mongo DB
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
MongoDB gridfs
MongoDB gridfsMongoDB gridfs
MongoDB gridfs
 
Getting Started with MongoDB and NodeJS
Getting Started with MongoDB and NodeJSGetting Started with MongoDB and NodeJS
Getting Started with MongoDB and NodeJS
 
MongoDB Internals
MongoDB InternalsMongoDB Internals
MongoDB Internals
 
MongoDB Operations for Developers
MongoDB Operations for DevelopersMongoDB Operations for Developers
MongoDB Operations for Developers
 
Java driver for mongo db
Java driver for mongo dbJava driver for mongo db
Java driver for mongo db
 
Mongo sf easy java persistence
Mongo sf   easy java persistenceMongo sf   easy java persistence
Mongo sf easy java persistence
 
What's new in the MongoDB Java Driver (2.5)?
What's new in the MongoDB Java Driver (2.5)?What's new in the MongoDB Java Driver (2.5)?
What's new in the MongoDB Java Driver (2.5)?
 
Introducing Hibernate OGM: porting JPA applications to NoSQL, Sanne Grinovero...
Introducing Hibernate OGM: porting JPA applications to NoSQL, Sanne Grinovero...Introducing Hibernate OGM: porting JPA applications to NoSQL, Sanne Grinovero...
Introducing Hibernate OGM: porting JPA applications to NoSQL, Sanne Grinovero...
 
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDBJava Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
 
MongoDB
MongoDBMongoDB
MongoDB
 
Java development with MongoDB
Java development with MongoDBJava development with MongoDB
Java development with MongoDB
 
Using JPA applications in the era of NoSQL: Introducing Hibernate OGM
Using JPA applications in the era of NoSQL: Introducing Hibernate OGMUsing JPA applications in the era of NoSQL: Introducing Hibernate OGM
Using JPA applications in the era of NoSQL: Introducing Hibernate OGM
 
Gridfs and MongoDB
Gridfs and MongoDBGridfs and MongoDB
Gridfs and MongoDB
 
MongoDB- Crud Operation
MongoDB- Crud OperationMongoDB- Crud Operation
MongoDB- Crud Operation
 
MongoDB on EC2 and EBS
MongoDB on EC2 and EBSMongoDB on EC2 and EBS
MongoDB on EC2 and EBS
 
An Enterprise Architect's View of MongoDB
An Enterprise Architect's View of MongoDBAn Enterprise Architect's View of MongoDB
An Enterprise Architect's View of MongoDB
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
Introduction to column oriented databases
Introduction to column oriented databasesIntroduction to column oriented databases
Introduction to column oriented databases
 

Ähnlich wie Get expertise with mongo db

Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDBJava Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
MongoDB
 
MongoDB for Time Series Data Part 3: Sharding
MongoDB for Time Series Data Part 3: ShardingMongoDB for Time Series Data Part 3: Sharding
MongoDB for Time Series Data Part 3: Sharding
MongoDB
 

Ähnlich wie Get expertise with mongo db (20)

Mongodb workshop
Mongodb workshopMongodb workshop
Mongodb workshop
 
MongoDB and Node.js
MongoDB and Node.jsMongoDB and Node.js
MongoDB and Node.js
 
Java Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDBJava Persistence Frameworks for MongoDB
Java Persistence Frameworks for MongoDB
 
MongoDB 3.2 - a giant leap. What’s new?
MongoDB 3.2 - a giant leap. What’s new?MongoDB 3.2 - a giant leap. What’s new?
MongoDB 3.2 - a giant leap. What’s new?
 
mongodb-introduction
mongodb-introductionmongodb-introduction
mongodb-introduction
 
Webinar: Building Your First App in Node.js
Webinar: Building Your First App in Node.jsWebinar: Building Your First App in Node.js
Webinar: Building Your First App in Node.js
 
Webinar: Building Your First App in Node.js
Webinar: Building Your First App in Node.jsWebinar: Building Your First App in Node.js
Webinar: Building Your First App in Node.js
 
Back to Basics Webinar 2 - Your First MongoDB Application
Back to  Basics Webinar 2 - Your First MongoDB ApplicationBack to  Basics Webinar 2 - Your First MongoDB Application
Back to Basics Webinar 2 - Your First MongoDB Application
 
Back to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB ApplicationBack to Basics Webinar 2: Your First MongoDB Application
Back to Basics Webinar 2: Your First MongoDB Application
 
MongoDB for Time Series Data Part 3: Sharding
MongoDB for Time Series Data Part 3: ShardingMongoDB for Time Series Data Part 3: Sharding
MongoDB for Time Series Data Part 3: Sharding
 
Javantura v2 - Replication with MongoDB - what could go wrong... - Philipp Krenn
Javantura v2 - Replication with MongoDB - what could go wrong... - Philipp KrennJavantura v2 - Replication with MongoDB - what could go wrong... - Philipp Krenn
Javantura v2 - Replication with MongoDB - what could go wrong... - Philipp Krenn
 
MongoDB Auto-Sharding at Mongo Seattle
MongoDB Auto-Sharding at Mongo SeattleMongoDB Auto-Sharding at Mongo Seattle
MongoDB Auto-Sharding at Mongo Seattle
 
Mongo db dla administratora
Mongo db dla administratoraMongo db dla administratora
Mongo db dla administratora
 
How sitecore depends on mongo db for scalability and performance, and what it...
How sitecore depends on mongo db for scalability and performance, and what it...How sitecore depends on mongo db for scalability and performance, and what it...
How sitecore depends on mongo db for scalability and performance, and what it...
 
Python mongo db-training-europython-2011
Python mongo db-training-europython-2011Python mongo db-training-europython-2011
Python mongo db-training-europython-2011
 
MongoDB and DynamoDB
MongoDB and DynamoDBMongoDB and DynamoDB
MongoDB and DynamoDB
 
Percona Toolkit for Effective MySQL Administration
Percona Toolkit for Effective MySQL AdministrationPercona Toolkit for Effective MySQL Administration
Percona Toolkit for Effective MySQL Administration
 
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
Webinaire 2 de la série « Retour aux fondamentaux » : Votre première applicat...
 
Effectively Scale and Operate AEM with MongoDB by Norberto Leite
Effectively Scale and Operate AEM with MongoDB by Norberto LeiteEffectively Scale and Operate AEM with MongoDB by Norberto Leite
Effectively Scale and Operate AEM with MongoDB by Norberto Leite
 
Mongo db roma replication and sharding
Mongo db roma replication and shardingMongo db roma replication and sharding
Mongo db roma replication and sharding
 

Mehr von Amit Thakkar (10)

Packer
PackerPacker
Packer
 
Packer
PackerPacker
Packer
 
AWS Cloud Formation
AWS Cloud FormationAWS Cloud Formation
AWS Cloud Formation
 
Introduction to node js
Introduction to node jsIntroduction to node js
Introduction to node js
 
Java script at backend nodejs
Java script at backend   nodejsJava script at backend   nodejs
Java script at backend nodejs
 
A different thought angular js part-3
A different thought   angular js part-3A different thought   angular js part-3
A different thought angular js part-3
 
A different thought angular js part-2
A different thought   angular js part-2A different thought   angular js part-2
A different thought angular js part-2
 
A different thought AngularJS
A different thought AngularJSA different thought AngularJS
A different thought AngularJS
 
Building user interface with react
Building user interface with reactBuilding user interface with react
Building user interface with react
 
Design pattern in an expressive language java script
Design pattern in an expressive language java scriptDesign pattern in an expressive language java script
Design pattern in an expressive language java script
 

Kürzlich hochgeladen

If this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New NigeriaIf this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New Nigeria
Kayode Fayemi
 
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
Sheetaleventcompany
 
Uncommon Grace The Autobiography of Isaac Folorunso
Uncommon Grace The Autobiography of Isaac FolorunsoUncommon Grace The Autobiography of Isaac Folorunso
Uncommon Grace The Autobiography of Isaac Folorunso
Kayode Fayemi
 
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptxChiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
raffaeleoman
 

Kürzlich hochgeladen (20)

If this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New NigeriaIf this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New Nigeria
 
BDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort Service
 
Report Writing Webinar Training
Report Writing Webinar TrainingReport Writing Webinar Training
Report Writing Webinar Training
 
Thirunelveli call girls Tamil escorts 7877702510
Thirunelveli call girls Tamil escorts 7877702510Thirunelveli call girls Tamil escorts 7877702510
Thirunelveli call girls Tamil escorts 7877702510
 
Air breathing and respiratory adaptations in diver animals
Air breathing and respiratory adaptations in diver animalsAir breathing and respiratory adaptations in diver animals
Air breathing and respiratory adaptations in diver animals
 
SaaStr Workshop Wednesday w/ Lucas Price, Yardstick
SaaStr Workshop Wednesday w/ Lucas Price, YardstickSaaStr Workshop Wednesday w/ Lucas Price, Yardstick
SaaStr Workshop Wednesday w/ Lucas Price, Yardstick
 
Aesthetic Colaba Mumbai Cst Call girls 📞 7738631006 Grant road Call Girls ❤️-...
Aesthetic Colaba Mumbai Cst Call girls 📞 7738631006 Grant road Call Girls ❤️-...Aesthetic Colaba Mumbai Cst Call girls 📞 7738631006 Grant road Call Girls ❤️-...
Aesthetic Colaba Mumbai Cst Call girls 📞 7738631006 Grant road Call Girls ❤️-...
 
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdfAWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
 
Dreaming Music Video Treatment _ Project & Portfolio III
Dreaming Music Video Treatment _ Project & Portfolio IIIDreaming Music Video Treatment _ Project & Portfolio III
Dreaming Music Video Treatment _ Project & Portfolio III
 
Introduction to Prompt Engineering (Focusing on ChatGPT)
Introduction to Prompt Engineering (Focusing on ChatGPT)Introduction to Prompt Engineering (Focusing on ChatGPT)
Introduction to Prompt Engineering (Focusing on ChatGPT)
 
ICT role in 21st century education and it's challenges.pdf
ICT role in 21st century education and it's challenges.pdfICT role in 21st century education and it's challenges.pdf
ICT role in 21st century education and it's challenges.pdf
 
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
 
Uncommon Grace The Autobiography of Isaac Folorunso
Uncommon Grace The Autobiography of Isaac FolorunsoUncommon Grace The Autobiography of Isaac Folorunso
Uncommon Grace The Autobiography of Isaac Folorunso
 
Dreaming Marissa Sánchez Music Video Treatment
Dreaming Marissa Sánchez Music Video TreatmentDreaming Marissa Sánchez Music Video Treatment
Dreaming Marissa Sánchez Music Video Treatment
 
Causes of poverty in France presentation.pptx
Causes of poverty in France presentation.pptxCauses of poverty in France presentation.pptx
Causes of poverty in France presentation.pptx
 
My Presentation "In Your Hands" by Halle Bailey
My Presentation "In Your Hands" by Halle BaileyMy Presentation "In Your Hands" by Halle Bailey
My Presentation "In Your Hands" by Halle Bailey
 
Presentation on Engagement in Book Clubs
Presentation on Engagement in Book ClubsPresentation on Engagement in Book Clubs
Presentation on Engagement in Book Clubs
 
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptxChiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
 
BDSM⚡Call Girls in Sector 97 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 97 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 97 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 97 Noida Escorts >༒8448380779 Escort Service
 
Busty Desi⚡Call Girls in Sector 51 Noida Escorts >༒8448380779 Escort Service-...
Busty Desi⚡Call Girls in Sector 51 Noida Escorts >༒8448380779 Escort Service-...Busty Desi⚡Call Girls in Sector 51 Noida Escorts >༒8448380779 Escort Service-...
Busty Desi⚡Call Girls in Sector 51 Noida Escorts >༒8448380779 Escort Service-...
 

Get expertise with mongo db

  • 1. Get Expertise with MongoDB Design Pattern
  • 2. Agenda ★ MongoDB Recap ★ How mongoDB works? ★ The _id ★ In n Out of Query Execution. ★ Indexes ★ What is Replication? ★ What is Sharding ?
  • 3. About Us Amit Thakkar Tech Blogger @ CodeChutney.in JavaScript Lover Working on MEAN Stack Twitter: @amit_thakkar01 LinkedIn: linkedin.com/in/amitthakkar01 Facebook: facebook.com/amit.thakkar01 Vibhor Kukreja JavaScript Ninja Working on MEAN Stack Twitter: @VibhorKukreja Email : vibhor.kukreja@intelligrape.com
  • 4. MongoDB Recap 1. No-Sql Database. 2. Installation. 3. Basic CRUD operation. 4. Stores data in JSON. 5. Schemaless. 6. Great Performance. 7. No Joins. 8. Easily scalable.
  • 6. How mongoDB works? There is a process “mongod” that acts as a database server. This process attaches itself to a directory with the --dbpath option. The default dbpath is “/data/db”. And start listening to a specific port number via --port option.The default port is 27017. > mongod --dbpath ~/testDB --port 28080 You just need to create the testDB directory before allocating it to the mongod process.
  • 7.
  • 8. Look what’s inside the created dbpath ? - /journal - test.ns // namespace for database - test.0 // raw data storage files - test.1 // raw data storage files But what is this journal directory for?
  • 9. Journal Option Journal option does a write ahead logging to an ondisk journal to guarantee write operation. i.e write in memory and on-disk journal before in the data file. ( Data Consistency ) Where can we set this option? There is an configuration file for the mongod process. > cat /etc/mongodb.conf
  • 10. How to get connected with mongod? There are two modes to interact with the DB. 1. mongoDB Driver/Plugin - Node.js (mongoose) - Grails (GORM) - Python (pymongo) 2. mongo Shell - supports javascript <3
  • 11. mongo Shell To connect a mongo shell process with our mongod process, we just have to specify the port number on which the mongod process is listening. > mongo --port 28080 To check the status of our mongo server, for a proper analysis we must start the mongostat process to check the server request/response. > mongostat --port 28080
  • 12. Yeah !! Our Single instance Server is Up and Running. At this point of time , we have just created a single mongoDB server along with a mongo shell that interacts with this mongoDB server.
  • 13. The _id It can be represented as a primary key in an collection. The mongo _id have few properties like, It must be unique. It can store any type of data, except boolean and array. eg - _id : “intelligrape” _id : { “company” : “intelligrape” } _id : 17 _id : true //You can do this, but in this case you can have only two records in this collection By default it stores ObjectId(“HexString”), and it gives a unique value every time as it is computed with the help of the following. ● The first 4 bytes representing the seconds ● The next 3 bytes are the machine identifier ● The next 2 bytes consists of process id ● The last 3 bytes are a random counter value
  • 14. _id impact on save() and insert() Query save( ) - is risky :P If there is an particular insert key already present in the collection, then it will replace that old document completely from the collection with the new one. ( without even prompting it :D ) So, it may become risky to use save query in our Applications. Its better to use the insert() query as it throws “duplicate key error index” error.
  • 15. Importing a document in mongoDB We can easily import a document in mongoDB. Mongo itself provides a process to import document. > mongoimport --db test --port 28080 --type json --collection person < testColl.json --db defines the database name --port server port number --type document type, which is to be import --collection name of the collection, where insertion is to be done
  • 16. PowerOf2Sizes ( v2.6 +) What is move and padding in mongoDB storage? > db.runCommand({ collMod : ”collection_Name”,usePowerOf2Sizes : true })
  • 17. In n Out of Query Execution. > db.person.find({}).sort({name:1}).skip(2).limit(3); > db.person.find({}).skip(2).sort({name:1}).limit(3); > db.person.find({}).limit(3).skip(2).sort({name:1}); What impact does they will make, on the result?
  • 18.
  • 19. Bulk Write Operations ( v2.6+) Type of bulk operations - ● Ordered ● Unordered > var bulk = db.person.initializeUnorderedBulkOp(); > bulk.insert( { name : ”Joe” } ); > bulk.insert( { name : ”Tim” } ); > bulk.insert( { name : ”Steve” } ); > bulk.execute();
  • 20. What is an Index? A data structure that can be used to make certain queries more efficient.
  • 21.
  • 22. How can we index ? An index on _id is automatic. For more, ensureIndex : > db.posts.ensureIndex({“name”:1}); > db.posts.ensureIndex({name: 1, date: ‐1}); > db.posts.ensureIndex({title: 1},{unique: true, sparse:true,dropDups:true}); > db.posts.ensureIndex(...,{background: true}); > db.posts.ensureIndex({“comments.author”: 1}); {“tags”: [“mongodb”, “indexing”], ...} > db.posts.ensureIndex({“tags”: 1}); // multikey > db.posts.ensureIndex({“location”: “2d”}); //geospatial db.post.find({title:”mongo”}).hint({name:1,title:1});
  • 23. What is Replication? ● Replication is the process of synchronizing data across multiple servers. ● Mongo achieves Replication through Replica Sets ● Replica sets are a form of asynchronous master/slave replication ● A replica set consists of two or more nodes that are copies of each other. (i.e.: replicas)
  • 24.
  • 25. Purpose of Replication ● Automated Failover / High Availability If primary fails then replica set will attempt to select another member to become the new primary. Use heartbeat signal to detect failure
  • 26. ● Distributed Read Load/Read Scaling By default, the primary node of a replica set is accessed for all reads and writes. ● Disaster Recovery So, you have a pool of servers with one primary (the master) and N secondaries (slaves). If the primarycrashes or disappears, the other servers will hold an election to choose a new primary. Arbiter helps to achieve >50 votes in case of draw. connectionString = (“mongodb://localhost:37017", replicaSet="s0",w=4, j=True)
  • 27. Lets Create a Replica Set :P mkdir -p ~/tempDB/rs1 ~/tempDB/rs2 ~/tempDB/rs3 mongod --replSet m101 --logpath "1.log" --dbpath ~/tempDB/rs1 --port 47017 --fork mongod --replSet m101 --logpath "2.log" --dbpath ~/tempDB/rs2 --port 47018 --fork mongod --replSet m101 --logpath "3.log" --dbpath ~/tempDB/rs3 --port 47019 --fork Seperate mongod processes have been created, lets connect them with each other.
  • 28. Configuring Replica Sets. > mongo --port 47017 config = { _id: "m101", members:[ { _id : 0, host : "localhost:47017"}, { _id : 1, host : "localhost:47018"}, { _id : 2, host : "localhost:47019"} ] }; rs.initiate(config); rs.status();
  • 29.
  • 30.
  • 32.
  • 33. Lets Create Shards :P Shard 0 mkdir -p ~/testDB/shard0/rs0 ~/testDB/shard0/rs1 ~/testDB/shard0/rs2 mongod --replSet s0 --logpath "testDB/s0-r0.log" --dbpath ~/testDB/shard0/rs0 --port 37017 --fork -- shardsvr mongod --replSet s0 --logpath "testDB/s0-r1.log" --dbpath ~/testDB/shard0/rs1 --port 37018 --fork -- shardsvr mongod --replSet s0 --logpath "testDB/s0-r2.log" --dbpath ~/testDB/shard0/rs2 --port 37019 --fork -- shardsvr mongo --port 37017 config = { _id: "s0", members:[ { _id : 0, host : "localhost:37017" }, { _id : 1, host : "localhost:37018" }, { _id : 2, host : "localhost:37019" }]}; rs.initiate(config);
  • 34. Shard 1 mkdir -p ~/testDB/shard1/rs0 ~/testDB/shard1/rs1 ~/testDB/shard1/rs2 mongod --replSet s1 --logpath "testDB/s1-r0.log" --dbpath ~/testDB/shard1/rs0 --port 47017 --fork -- shardsvr mongod --replSet s1 --logpath "testDB/s1-r1.log" --dbpath ~/testDB/shard1/rs1 --port 47018 --fork -- shardsvr mongod --replSet s1 --logpath "testDB/s1-r2.log" --dbpath ~/testDB/shard1/rs2 --port 47019 --fork -- shardsvr mongo --port 47017 config = { _id: "s1", members:[ { _id : 0, host : "localhost:47017" }, { _id : 1, host : "localhost:47018" }, { _id : 2, host : "localhost:47019" }]}; rs.initiate(config);
  • 35. Shard 2 mkdir -p ~/testDB/shard2/rs0 ~/testDB/shard2/rs1 ~/testDB/shard2/rs2 mongod --replSet s2 --logpath "testDB/s2-r0.log" --dbpath ~/testDB/shard2/rs0 --port 57017 --fork -- shardsvr mongod --replSet s2 --logpath "testDB/s2-r1.log" --dbpath ~/testDB/shard2/rs1 --port 57018 --fork -- shardsvr mongod --replSet s2 --logpath "testDB/s2-r2.log" --dbpath ~/testDB/shard2/rs2 --port 57019 --fork -- shardsvr mongo --port 57017 config = { _id: "s2", members:[ { _id : 0, host : "localhost:57017" }, { _id : 1, host : "localhost:57018" }, { _id : 2, host : "localhost:57019" }]}; rs.initiate(config);
  • 36. Congif Servers mkdir -p ~/testDB/config/config-a ~/testDB/config/config-b ~/testDB/config/config-c mongod --logpath "testDB/cfg-a.log" --dbpath ~/testDB/config/config-a --port 57040 - -fork --configsvr mongod --logpath "testDB/cfg-b.log" --dbpath ~/testDB/config/config-b --port 57041 - -fork --configsvr mongod --logpath "testDB/cfg-c.log" --dbpath ~/testDB/config/config-c --port 57042 - -fork --configsvr
  • 37. Setting Up “mongos” mongos --logpath "testDB/mongos-1.log" --port 28888 --configdb localhost:57040,localhost:57041,localhost:57042 --fork mongo --port 28888 db.adminCommand( { addshard : "s0/"+"localhost:37017" } ); db.adminCommand( { addshard : "s1/"+"localhost:47017" } ); db.adminCommand( { addshard : "s2/"+"localhost:57017" } ); db.adminCommand({enableSharding: "test"}) // test is the db name db.adminCommand({shardCollection: "test.grades", key: {month:1,student_id:1}});
  • 39. Congo !! Your Shard environment is up and running :)