11
Neha Nivedita
Getting Ahead With MongoDB
Development | Consulting | Training
PARTNERS, ALLIANCES & OFFICES
33
INDIA
AUSTRALIA
USA
NORWAY
S. AFRICA
SINGAPORE
ABOUT ME
44
Open Source Contribution
Node.js
React
Enzyme
Meteor
Full Stack Developer
JavaScript
Node.js
React
MongoDB
MEAN
Speaker
JS Fusion
Meteor Noida
DeveloperWeek
MongoDB World
MongoDB Certified
Developer
Trainer
Points of Discussion
55
What’s MongoDB
How to use it
Effective DB Design Nexus Architecture
Building scalable apps Bonus Section!
What is MongoDB?
Some Popular Databases
7
WHAT IS MONGODB?
7
Oracle
MySQL
MSSQL
PostgreSQL
MongoDB
Architecture
8
WHAT IS MONGODB?
8
MongoDB Document
{
name : “Black Panther”,
title : “King”,
address : {
address1: “Throne Room”,
address2: “Royal Palace”,
country : “Wakanda”,
}
associates: [ “Shuri”, “Okoye”, “Nakia”, “Ramonda”],
punches_dropped : 1734879,
}
key value
string
nested document
or object
array
integer
WHAT IS MONGODB?
Why use MongoDB?
10
WHAT IS MONGODB?
10
Schemaless
JavaScript
everywhere!
Fast IO
Horizontal
scaling
Vertical Scaling
11
WHAT IS MONGODB?
11
Horizontal Scaling
12
WHAT IS MONGODB?
12
RECAP
13
WHAT IS MONGODB?
13
Documents, collections & database
Benefits of MongoDB
Scalability
How to use MongoDB
MongoDB Query Language
15
HOW TO USE MONGODB?
15
Query syntax:
db.<collection>.<operation>({ <key>: <value> });
CRUD Operations
16
HOW TO USE MONGODB?
16
Create:
db.avengers.insert({ name: ‘thor’ });
{
_id: 1,
name: ‘thor’
}
avengers
CRUD Operations
17
HOW TO USE MONGODB?
17
Read:
db.avengers.find({ name: ‘thor’ });
{
_id: 1,
name: ‘thor’
}
avengers
[{
_id: 1,
name: ‘thor’
}]
Result set:
CRUD Operations
18
HOW TO USE MONGODB?
18
Update:
db.avengers.update({ name: ‘thor’ }, { brother: ‘loki’ });
{
_id: 1,
name: ‘thor’,
brother: ‘loki’
}
avengers
{
_id: 1,
name: ‘thor’
}
avengers
CRUD Operations
19
HOW TO USE MONGODB?
19
Delete:
db.avengers.remove({ name: ‘thor’ });
avengers
{
_id: 1,
name: ‘thor’,
brother: ‘loki’
}
avengers
Cursor Operations
20
HOW TO USE MONGODB?
20
Count: db.avengers.find({ team: ‘Iron Man’ }).count();
Sort: db.avengers.find({ team: ‘Iron Man’ }).sort({ name: -1 });
Skip: db.avengers.find({ team: ‘Iron Man’ }).skip(5);
Limit: db.avengers.find({ team: ‘Iron Man’ }).limit(10);
Aggregation
21
HOW TO USE MONGODB?
21
{ _id: 1, name: ‘Captain America’, location: “Earth”, punches: 1273453, team: “Cap” },
{ _id: 2, name: ‘Iron Man’, location: “Earth”, punches: 798712, team: “Iron Man” },
{ _id: 3, name: ‘The Vision’, location: “Earth”, punches: 3465, team: “Iron Man” },
{ _id: 4, name: ‘Bucky’, location: “Earth”, punches: 9732, team: “Cap” },
{ _id: 5, name: ‘Black Widow’, location: “Earth”, punches: 923213, team: “Iron Man” },
{ _id: 6, name: ‘Hawkeye’, location: “Earth”, punches: 8745, team: “Cap” },
{ _id: 7, name: ‘Thor’, location: “Asgard”, punches: 8721345, team: “None” },
{ _id: 8, name: ‘Hulk’, location: “Nobody knows”, punches: 4097392, team: “Hulk” },
collection: avengers
Aggregation
22
HOW TO USE MONGODB?
22
db.avengers.aggregate([
{ $match: { location: "Earth" } },
{ $group: {
_id: "$team",
total_punches: { $sum: "$punches" }
}
},
{ $sort: { total_punches: -1 } }
]);
Pipeline stages: $match -> $group -> $sort
[
{
_id: ‘Iron Man’,
total_punches: 1725390
},
{
_id: ‘Cap’,
total_punches: 1291930
}
]
Result set:
RECAP
23
HOW TO USE MONGODB?
23
CRUD operations
Cursor operations
Aggregation
Effective DB Design
Thought Transformation
25
EFFECTIVE DB DESIGN
25
Degree of
association
Relational
or not?
Understand your
data & think
differently
Is NoSQL right
for your use
case?
How deeply
relational is your
data?
01 02 03 To what extent
each document can
be denormalized?
How many relations
exist per
document?
Denormalize properly
26
EFFECTIVE DB DESIGN
26
shows
castMembers
reviews
episodes
seasons
many
many
many
many
What not to do
27
EFFECTIVE DB DESIGN
27
{
title: 'Agents of SHIELD',
seasons: [
{
seasonNumber: 1,
episodes: [
{
episodeNumber: 1,
title: 'Pilot',
castMembers: [{ name: 'Chloe Bennet', ... }, {...}],
reviews: [{ author: ‘Stan Lee’, stars: 4, ... }, {...}],
}
]
}
]
}
Understand your data
28
EFFECTIVE DB DESIGN
28
Degree of relation
Embedded documents
References
One to One
29
MODELLING
29
One to Few
30
MODELLING
30
One to Many
31
MODELLING
31
One to Squillions
32
MODELLING
32
Use Indexes Correctly
33
EFFECTIVE DB DESIGN
33
Covered queries
Compound indexes
Sorting with indexes
Use query analysis: .explain()
RECAP
34
EFFECTIVE DB DESIGN
34
Model data correctly!
Contemplate the N in one-to-N!
Keep your queries covered!
Building Scalable Apps
Building scalable apps
36
BUILDING SCALABLE APPS
36
Choosing
Shard Key
Sharding
Replication
When to
Shard Scale easy!
Replica Set
37
BUILDING SCALABLE APPS
37
Replica Set
38
BUILDING SCALABLE APPS
38
Replica Set
39
BUILDING SCALABLE APPS
39
Shards
40
BUILDING SCALABLE APPS
40
Shards
41
BUILDING SCALABLE APPS
41
Shards
42
BUILDING SCALABLE APPS
42
Shards
43
BUILDING SCALABLE APPS
43
Choosing a Shard Key
44
BUILDING SCALABLE APPS
44
Shard Key determines how your data is divided
Create a Shard Key that targets a single shard
High degree of randomness
Easily divisible shards
When to Shard
45
BUILDING SCALABLE APPS
45
Plan ahead
Choose a shard key wisely - it cannot be changed
You cannot unshard a sharded collection
RECAP
46
BUILDING SCALABLE APPS
46
Use replicas for high availability
Shard your data for high scalability
Choose shard keys correctly!
Nexus Architecture
Nexus Architecture
48
ARCHITECTURE
48
Transactions (in 4.0!)
Transactions in 4.0
50
TRANSACTIONS
50
Beta documentation is out
Similar to SQL transactions
Has some limitations
Transactions in 4.0
51
TRANSACTIONS
51
Start client session:
client_session = client.start_session()
Transactions in 4.0
52
TRANSACTIONS
52
Start transaction:
client_session.start_transaction()
Transactions in 4.0
53
TRANSACTIONS
53
Pass the session to each transaction operation:
db.collection.insert_one({... }, session=client_session)
db.collection.insert_one({...}, session=client_session)
[...]
db.collection.update_many({...}, {...}, session=client_session)
my_doc = db.collection.find_one({...}, session=client_session)
Transactions in 4.0
54
TRANSACTIONS
54
Commit or rollback to end the transaction:
client_session.commit_transaction()
OR
client_session.abort_transaction()
Transactions in 4.0
55
TRANSACTIONS
55
Runtime limit < 1 min
Oplog size limit < 16 MB
Beta testing starts soon
56
THANK YOU
Neha Nivedita @nodexperts
neha@nodexperts.com www.nodexperts.com

Getting Ahead with MongoDB