From SQL to NoSQL—
Changing Your Mindset
@Lauren_Schaefer, Developer Advocate,
MongoDB
Parks and Recreation, Season 6, Episode 14
Parks and Recreation, Season 6, Episode 14
Lauren Schaefer
Developer Advocate, MongoDB
@Lauren_Schaefer
From SQL to NoSQL—
Changing Your Mindset
Make the leap from SQL to
MongoDB1. Map terms & concepts from SQL to MongoDB
2. Discover the 4 humongous advantages of MongoDB
3. Change your mindset in 3 key ways
Make the leap from SQL to
MongoDB1. Map terms & concepts from SQL to MongoDB
2. Discover the 4 humongous advantages of MongoDB
3. Change your mindset in 3 key ways
MongoDB stores data in documents
MongoDB stores data in documents
{
first_name: "Paul",
surname: "Miller",
cell: "447557505611",
city: "London",
location: [45.123,47.232],
profession: ["banking", "finance", "trader"],
cars: [
{
model: "Bentley",
year: 1973
},
{
model: "Rolls Royce",
year: 1965
}
]
}
#MDBlocal @Lauren_Schaef
MongoDB stores data in documents
{
first_name: "Paul",
surname: "Miller",
cell: "447557505611",
city: "London",
location: [45.123,47.232],
profession: ["banking", "finance", "trader"],
cars: [
{
model: "Bentley",
year: 1973
},
{
model: "Rolls Royce",
year: 1965
}
]
}
#MDBlocal @Lauren_Schaef
MongoDB stores data in documents
{
first_name: "Paul",
surname: "Miller",
cell: "447557505611",
city: "London",
location: [45.123,47.232],
profession: ["banking", "finance", "trader"],
cars: [
{
model: "Bentley",
year: 1973
},
{
model: "Rolls Royce",
year: 1965
}
]
}
#MDBlocal @Lauren_Schaef
MongoDB stores data in documents
{
first_name: "Paul",
surname: "Miller",
cell: "447557505611",
city: "London",
location: [45.123,47.232],
profession: ["banking", "finance", "trader"],
cars: [
{
model: "Bentley",
year: 1973
},
{
model: "Rolls Royce",
year: 1965
}
]
}
#MDBlocal @Lauren_Schaef
MongoDB stores data in documents
{
first_name: "Paul",
surname: "Miller",
cell: "447557505611",
city: "London",
location: [45.123,47.232],
profession: ["banking", "finance", "trader"],
cars: [
{
model: "Bentley",
year: 1973
},
{
model: "Rolls Royce",
year: 1965
}
]
}
#MDBlocal @Lauren_Schaef
MongoDB stores data in documents
{
first_name: "Paul",
surname: "Miller",
cell: "447557505611",
city: "London",
location: [45.123,47.232],
profession: ["banking", "finance", "trader"],
cars: [
{
model: "Bentley",
year: 1973
},
{
model: "Rolls Royce",
year: 1965
}
]
}
#MDBlocal @Lauren_Schaef
MongoDB stores data in documents
{
first_name: "Paul",
surname: "Miller",
cell: "447557505611",
city: "London",
location: [45.123,47.232],
profession: ["banking", "finance", "trader"],
cars: [
{
model: "Bentley",
year: 1973
},
{
model: "Rolls Royce",
year: 1965
}
]
}
#MDBlocal @Lauren_Schaef
MongoDB stores data in documents
{
first_name: "Paul",
surname: "Miller",
cell: "447557505611",
city: "London",
location: [45.123,47.232],
profession: ["banking", "finance", "trader"],
cars: [
{
model: "Bentley",
year: 1973
},
{
model: "Rolls Royce",
year: 1965
}
]
}
#MDBlocal @Lauren_Schaef
Modeling data in MongoDB vs
SQL
{
first_name: "Paul",
surname: "Miller",
cell: "447557505611",
city: "London",
location: [45.123,47.232],
profession: ["banking", "finance", "trader"],
cars: [
{
model: "Bentley",
year: 1973
},
{
model: "Rolls Royce",
year: 1965
}
]
}
#MDBlocal @Lauren_Schaef
Modeling data in MongoDB vs
SQL
{
first_name: "Paul",
surname: "Miller",
cell: "447557505611",
city: "London",
location: [45.123,47.232],
profession: ["banking", "finance", "trader"],
cars: [
{
model: "Bentley",
year: 1973
},
{
model: "Rolls Royce",
year: 1965
}
]
}
ID first_name surname cell city location_x location_y
1 Paul Miller 447557505611 London 45.123 47.232
Users
#MDBlocal @Lauren_Schaef
Modeling data in MongoDB vs
SQL
{
first_name: "Paul",
surname: "Miller",
cell: "447557505611",
city: "London",
location: [45.123,47.232],
profession: ["banking", "finance", "trader"],
cars: [
{
model: "Bentley",
year: 1973
},
{
model: "Rolls Royce",
year: 1965
}
]
}
ID first_name surname cell city location_x location_y
1 Paul Miller 447557505611 London 45.123 47.232
Users
ID user_id profession
10 1 banking
11 1 finance
12 1 trader
Professions
#MDBlocal @Lauren_Schaef
Modeling data in MongoDB vs
SQL
{
first_name: "Paul",
surname: "Miller",
cell: "447557505611",
city: "London",
location: [45.123,47.232],
profession: ["banking", "finance", "trader"],
cars: [
{
model: "Bentley",
year: 1973
},
{
model: "Rolls Royce",
year: 1965
}
]
}
ID user_id profession
10 1 banking
11 1 finance
12 1 trader
Professions
ID user_id model year
20 1 Bentley 1973
21 1 Rolls Royce 1965
Cars
ID first_name surname cell city location_x location_y
1 Paul Miller 447557505611 London 45.123 47.232
Users
#MDBlocal @Lauren_Schaef
Modeling data in MongoDB vs
SQL
{
first_name: "Paul",
surname: "Miller",
cell: "447557505611",
city: "London",
location: [45.123,47.232],
profession: ["banking", "finance", "trader"],
cars: [
{
model: "Bentley",
year: 1973
},
{
model: "Rolls Royce",
year: 1965
}
]
}
ID user_id profession
10 1 banking
11 1 finance
12 1 trader
Professions
ID user_id model year
20 1 Bentley 1973
21 1 Rolls Royce 1965
Cars
ID first_name surname cell city location_x location_y
1 Paul Miller 447557505611 London 45.123 47.232
Users
#MDBlocal @Lauren_Schaef
Collections vs Tables
{
first_name: "Paul",
surname: "Miller",
cell: "447557505611",
city: "London",
location: [45.123,47.232],
profession: ["banking", "finance", "trader"],
cars: [
{
model: "Bentley",
year: 1973
},
{
model: "Rolls Royce",
year: 1965
}
]
}
{
first_name: ”Lauren",
surname: ”Schaefer",
cell: ”1235552222",
city: ”Lancaster",
profession: [”software engineer", ”developer advocate"],
}
{
first_name: ”Sydney",
surname: ”Schaefer",
city: ”Lancaster",
school: ”Daisy’s Daycare”
}
ID first_name surname cell city location_x location_y
1 Paul Miller 447557505611 London 45.123 47.232
2 Lauren Schaefer 1235552222 Lancaster NULL NULL
3 Sydney Schaefer NULL Lancaster NULL NULL
UsersUsers
#MDBlocal @Lauren_Schaef
ID first_name surname cell city location_x location_y
1 Paul Miller 447557505611 London 45.123 47.232
2 Lauren Schaefer 1235552222 Lancaster NULL NULL
3 Sydney Schaefer NULL Lancaster NULL NULL
Collections vs Tables
{
first_name: "Paul",
surname: "Miller",
cell: "447557505611",
city: "London",
location: [45.123,47.232],
profession: ["banking", "finance", "trader"],
cars: [
{
model: "Bentley",
year: 1973
},
{
model: "Rolls Royce",
year: 1965
}
]
}
{
first_name: ”Lauren",
surname: ”Schaefer",
cell: ”1235552222",
city: ”Lancaster",
profession: [”software engineer", ”developer advocate"],
}
{
first_name: ”Sydney",
surname: ”Schaefer",
city: ”Lancaster",
school: ”Daisy’s Daycare”
}
UsersUsers
#MDBlocal @Lauren_Schaef
Collections vs Tables
ID first_name surname cell city location_x location_y
1 Paul Miller 447557505611 London 45.123 47.232
2 Lauren Schaefer 1235552222 Lancaster NULL NULL
3 Sydney Schaefer NULL Lancaster NULL NULL
{
first_name: "Paul",
surname: "Miller",
cell: "447557505611",
city: "London",
location: [45.123,47.232],
profession: ["banking", "finance", "trader"],
cars: [
{
model: "Bentley",
year: 1973
},
{
model: "Rolls Royce",
year: 1965
}
]
}
{
first_name: ”Lauren",
surname: ”Schaefer",
cell: ”1235552222",
city: ”Lancaster",
profession: [”software engineer", ”developer advocate"],
}
{
first_name: ”Sydney",
surname: ”Schaefer",
city: ”Lancaster",
school: ”Daisy’s Daycare”
}
UsersUsers
#MDBlocal @Lauren_Schaef
Schemaless
database
#MDBlocal @Lauren_Schaef
Schemaless
database
Don’t panic!
Use schema validation.
#MDBlocal @Lauren_Schaef
Document Row
{
...
a: “b”
...
}
ID a ...
1 b ...
2 ... ...
3 ... ...
Document Row(s)
{
...
a: “b”
...
}
ID a ...
1 b ...
2 ... ...
3 ... ...
... ... ...
... ... ...
... ... ...
... ... ...
... ... ...
... ... ...
Field Column
ID a ...
1 b ...
2 c ...
3 ... ...
{
...
a: “b”
...
}
{
...
a: “c”
...
}
Collection Table
{
...
}
... ... ...
... ... ...
... ... ...
... ... ...
{
...
}
{
...
}
Database Database
... ... ...
... ... ...
... ... ...
... ... ...
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
... ... ...
... ... ...
... ... ...
... ... ...
... ... ...
Index Index
{
...
}
{
...
}
{
...
}
{
...
}
... ... ...
... ... ...
... ... ...
... ... ...
... ... ...
View View
{
...
}
... ... ...
... ... ...
... ... ...
... ... ...
{
...
}
{
...
}
Embedding Join
{
...
a: “b”,
...
c: {
d: “e”
...
},
...
}
ID a ...
1 b ...
2 ... ...
3 ... ...
... d ...
1 e ...
... ... ...
Database References Join
ID ... ...
1 ... ...
2 ... ...
3 ... ...
... ... ...
1 ... ...
... ... ...
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
$lookup
(Aggregation Pipeline)
Left Outer Join
ID ... ...
1 ... ...
2 ... ...
3 ... ...
... ... ...
1 ... ...
4 ... ...
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
$graphLookup
(Aggregation Pipeline)
Recursive Common
Table Expressions
{
...
}
... ... ...
... ... ...
... ... ...
... ... ...
{
...
}
{
...
}
Multi-Document ACID
Transaction
Multi-Record ACID
Transaction
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
... ... ...
... ... ...
... ... ...
... ... ...
... ... ...
... ... ...
... ... ...
Term mapping summary
x
Row Column Table Database Index Join Join
Left Outer
Join
Recursive
Common Table
Expressions
View Transaction
Document Field Collection Database Index Embedding
Database
References
$lookup $graphLookup View Transaction
#MDBlocal @Lauren_Schaef
Make the leap from SQL to MongoDB
1. Map terms & concepts from SQL to MongoDB
2. Discover the 4 humongous advantages of MongoDB
3. Change your mindset in 3 key ways
#MDBlocal @Lauren_Schaef
Scale cheaper!
As the size of your database grows, scale horizontally.
#MDBlocal @Lauren_Schaef
SQLMongoDB
Query faster!
Stop doing expensive joins to get your data.
ID a ...
1 b ...
2 ... ...
3 ... ...
... ... ...
... ... ...
... ... ...
... ... ...
... ... ...
... ... ...
{
...
a: “b”,
...
c: {
d: “e”
...
},
...
}
#MDBlocal @Lauren_Schaef
SQLMongoDB
Pivot easier!
Easily change the shape of your data as your app evolves.
{
a: “b”,
c: “one”,
e: “f”
}
{
a: “b”,
c: 1,
new: “no biggee”
}
ALTER TABLE `mydb`.`letters_table`
DROP COLUMN `e`,
ADD COLUMN `New` VARCHAR(45) NULL AFTER `C`,
CHANGE COLUMN `C` `C` INT NULL DEFAULT NULL ;
#MDBlocal @Lauren_Schaef
Program faster!
Documents map to data structures in most popular languages.
Update Your Profile
#MDBlocal @Lauren_Schaef
Program faster!
{
first_name: "Paul",
surname: "Miller",
cell: "447557505611",
city: "London",
location: [45.123,47.232],
profession: ["banking", "finance", "trader"],
}
ID first_name surname cell city location_x location_y
1 Paul Miller 447557505611 London 45.123 47.232
Users
ID user_id profession
10 1 banking
11 1 finance
12 1 trader
Professions
#MDBlocal @Lauren_Schaef
Program faster!
import pymongo
from pymongo import MongoClient
# CONNECT TO THE DB
client = MongoClient()
client =
pymongo.MongoClient("mongodb+srv://
username:password@cluster0nsdia.
mongodb.net/test?retryWrites=true")
db = client.fabapp
# THE ID OF THE USER WHOSE PROFILE WE
# WILL BE RETRIEVING AND UPDATING
userId = 1
import mysql.connector
# CONNECT TO THE DB
mydb = mysql.connector.connect(
host="localhost",
user=”username",
passwd=”password",
database=”fabapp”
)
mycursor = mydb.cursor()
# THE ID OF THE USER WHOSE PROFILE WE
# WILL BE RETRIEVING AND UPDATING
userId = 1
Program faster!
# GET THE USER'S PROFILE INFORMATION
## We can pull all of the info from
## the same document since we used
## embedding
user =
db['Users'].find_one({"_id":userId})
# GET THE USER'S PROFILE INFORMATION
### Pull the info from the Users table
### & put it in the user dictionary
sql = "Select * FROM Users WHERE
Users.ID=%s”
values = (userId,)
mycursor.execute(sql, values)
result = mycursor.fetchone()
user = {
"first_name": result[1],
"surname": result[2],
"cell": result[3],
"city": result[4],
"location_x": result[5],
"location_y": result[6]
}
### Pull the info from the Professions
### table & put it in the user dict.
sql = "Select * FROM Professions WHERE
Professions.user_id=%s”
values = (userId,)
mycursor.execute(sql, values)
results = mycursor.fetchall()
professions = []
for result in results:
professions.append(result[2])
user["professions"] = professions
Program faster!
# UPDATE THE USER DICTIONARY BASED ON
# USER INPUT IN THE APP
### We'll update the user dictionary
### manually for simplicity
user = {
"first_name": "NewFirst",
"surname": "NewSurname",
"cell": "123-456-7890",
"city": "NewCity",
"location": [40.762, -73.979],
"professions": ["Manager",
"Engineer"]
}
# UPDATE THE USER DICTIONARY BASED ON
# USER INPUT IN THE APP
### We'll update the user dictionary
### manually for simplicity
user = {
"first_name": "NewFirst",
"surname": "NewSurname",
"cell": "123-456-7890",
"city": "NewCity",
"location_x": 40.762,
"location_y": 73.979,
"professions": ["Manager",
"Engineer"]
}
Program faster!
# UPDATE THE USER'S PROFILE IN THE DB
### Since the user's data is stored in
### a single document, we only have to
### make one update
result = db['Users'].update_one(
{"_id": userId}, {"$set": user})
# UPDATE THE USER'S PROFILE IN THE DB
### First update what is stored in the
### Users table
sql = "UPDATE Users SET first_name=%s,
surname=%s, cell=%s, city=%s,
location_x=%s, location_y=%s
WHERE (ID=%s)"
values = (
user["first_name"],
user["surname"],
user["cell"],
user["city"],
user["location_x"],
user["location_y"],
userId)
mycursor.execute(sql, values)
mydb.commit()
### Delete existing records in
### Professions table and add new ones
sql = "DELETE FROM Professions WHERE
user_id=%s”
values = (userId,)
mycursor.execute(sql, values)
mydb.commit()
if(len(user["professions"]) > 0):
sql = "INSERT INTO Professions
(user_id,profession) VALUES
(%s, %s)”
values = []
for profession in user["professions"]:
values.append((userId, profession))
mycursor.executemany(sql,values)
mydb.commit()
Program faster!
30 lines of code 73 lines of code
Make the leap from SQL to MongoDB
1. Map terms & concepts from SQL to MongoDB
2. Discover the 4 humongous advantages of MongoDB
3. Change your mindset in 3 key ways
#MDBlocal @Lauren_Schaef
Embrace document diversity
#MDBlocal @Lauren_Schaef
Embrace document diversity
The Polymorphic Pattern
{
first_name: "Paul",
surname: "Miller",
cell: "447557505611",
city: "London",
location: [45.123,47.232],
profession: ["banking", "finance", "trader"],
cars: [
{
model: "Bentley",
year: 1973
},
{
model: "Rolls Royce",
year: 1965
}
]
}
{
first_name: ”Lauren",
surname: ”Schaefer",
cell: ”1235552222",
city: ”Lancaster",
profession: [”software engineer", ”developer advocate"],
}
{
first_name: ”Sydney",
surname: ”Schaefer",
city: ”Lancaster",
school: ”Daisy’s Daycare”
}
#MDBlocal @Lauren_Schaef
Embrace document diversity
The Outlier Pattern
{
_id: ”Lauren_Schaefer",
displayName: ”Lauren Schaefer",
numFollowers: 1310
followers: [
“naomi_pen”,
“kenwalger”,
“mylynn”
...
]
}
{
_id: ”Nick_Offerman",
displayName: ”Nick Offerman",
numFollowers: 1730332
followers: [
“c_hotaling”,
“IAmJerdog”,
“ChloeCondon”
...
],
has_extras: true
}
{
_id: ”Nick_Offerman_1",
twitter_id: “Nick_Offerman”,
is_overflow: true,
followers: [
“StephenAtHome”,
“TheEllenShow”,
“hulu”
...
]
}
#MDBlocal @Lauren_Schaef
Data that is accessed together
should be stored together
#MDBlocal @Lauren_Schaef
Data that is accessed together
should be stored together
#MDBlocal @Lauren_Schaef
0
20000
40000
60000
80000
100000
120000
1985 2017
Storage vs Developer Costs
Storage Cost per GB Developer Salary
Data that is accessed together
should be stored together
#MDBlocal @Lauren_Schaef
Don’t normalize your data for the sake of normalizing it.
{
a: “b”,
c: {
d: “e”
...
},
f: [“g”, “h”, “i”],
j: [
{
k: “l”
},
{
m: “n”
}
]
}
Data that is accessed together
should be stored together
#MDBlocal @Lauren_Schaef
Tread carefully with transactions
Relying on transactions is a bad design smell.
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
#MDBlocal @Lauren_Schaef
Make the leap from SQL to MongoDB
#MDBlocal @Lauren_Schaef
Make the leap from SQL to MongoDB
1. Map terms & concepts from SQL to MongoDB
x
Row Column Table Database Index Join Join
Left Outer
Join
Recursive
Common Table
Expressions
View Transaction
Document Field Collection Database Index Embedding
Database
References
$lookup $graphLookup View Transaction
#MDBlocal @Lauren_Schaef
Make the leap from SQL to MongoDB
1. Map terms & concepts from SQL to MongoDB
2. Discover the 4 humongous advantages of MongoDB
#MDBlocal @Lauren_Schaef
Make the leap from SQL to
MongoDB1. Map terms & concepts from SQL to MongoDB
2. Discover the 4 humongous advantages of MongoDB
1. Scale cheaper
#MDBlocal @Lauren_Schaef
Make the leap from SQL to MongoDB
1. Map terms & concepts from SQL to MongoDB
2. Discover the 4 humongous advantages of MongoDB
1. Scale cheaper
2. Query faster
#MDBlocal @Lauren_Schaef
Make the leap from SQL to MongoDB
1. Map terms & concepts from SQL to MongoDB
2. Discover the 4 humongous advantages of MongoDB
1. Scale cheaper
2. Query faster
3. Pivot easier
#MDBlocal @Lauren_Schaef
Make the leap from SQL to MongoDB
1. Map terms & concepts from SQL to MongoDB
2. Discover the 4 humongous advantages of MongoDB
1. Scale cheaper
2. Query faster
3. Pivot easier
4. Program faster
# GET THE USER'S PROFILE INFORMATION
## We can pull all of the info from
## the same document since we used
## embedding
user =
db['Users'].find_one({"_id":userId})
# GET THE USER'S PROFILE INFORMATION
### Pull the info from the Users table ### &
put it in the user dictionary
sql = "Select * FROM Users WHERE
Users.ID=%s”
values = (userId,)
mycursor.execute(sql, values)
result = mycursor.fetchone()
user = {
"first_name": result[1],
"surname": result[2],
"cell": result[3],
"city": result[4],
"location_x": result[5],
"location_y": result[6]
}
#MDBlocal @Lauren_Schaef
Make the leap from SQL to MongoDB
1. Map terms & concepts from SQL to MongoDB
2. Discover the 4 humongous advantages of MongoDB
3. Change your mindset in 3 key ways
#MDBlocal @Lauren_Schaef
Make the leap from SQL to MongoDB
1. Map terms & concepts from SQL to MongoDB
2. Discover the 4 humongous advantages of MongoDB
3. Change your mindset in 3 key ways
1. Embrace document diversity
#MDBlocal @Lauren_Schaef
Make the leap from SQL to
MongoDB1. Map terms & concepts from SQL to MongoDB
2. Discover the 4 humongous advantages of MongoDB
3. Change your mindset in 3 key ways
1. Embrace document diversity
2. Data that is accessed together should be stored together
Make the leap from SQL to MongoDB
1. Map terms & concepts from SQL to MongoDB
2. Discover the 4 humongous advantages of MongoDB
3. Change your mindset in 3 key ways
1. Embrace document diversity
2. Data that is accessed together should be stored together
#MDBlocal @Lauren_Schaef
Make the leap from SQL to MongoDB
1. Map terms & concepts from SQL to MongoDB
2. Discover the 4 humongous advantages of MongoDB
3. Change your mindset in 3 key ways
1. Embrace document diversity
2. Data that is accessed together should be stored together
3. Tread carefully with transactions
#MDBlocal @Lauren_Schaef
Don’t be Ron Swanson
(in this particular case)
#MDBlocal @Lauren_Schaef
Change your mindset &
get the full value of MongoDB
Don’t be Ron Swanson
#MDBlocal @Lauren_Schaef
Additional resources on data
modeling patterns
• Advanced Schema Design Patterns (webinar)
• Building with Patterns: A Summary (blog series)
• M320: Data Modeling (MongoDB University Course – brand new!)
#MDBlocal @Lauren_Schaef
Additional resources
• The MongoDB Docs
• JSON Schema Validation – Locking down your model the smart way
• JSON Schema Validation - Checking Your Arrays
• M121: The MongoDB Aggregation Framework
#MDBlocal @Lauren_Schaef
Don’t be Ron Swanson
(in this particular case)
Change your mindset and get the
full value of MongoDB
Change your mindset &
get the full value of MongoDB
Get the slides on my Twitter
page: @Lauren_Schaefer
#MDBlocal @Lauren_Schaef
Thank You!
Jumpstart! From SQL to NoSQL -- Changing Your Mindset

Jumpstart! From SQL to NoSQL -- Changing Your Mindset

  • 2.
    From SQL toNoSQL— Changing Your Mindset @Lauren_Schaefer, Developer Advocate, MongoDB
  • 3.
    Parks and Recreation,Season 6, Episode 14
  • 7.
    Parks and Recreation,Season 6, Episode 14
  • 8.
    Lauren Schaefer Developer Advocate,MongoDB @Lauren_Schaefer
  • 9.
    From SQL toNoSQL— Changing Your Mindset
  • 10.
    Make the leapfrom SQL to MongoDB1. Map terms & concepts from SQL to MongoDB 2. Discover the 4 humongous advantages of MongoDB 3. Change your mindset in 3 key ways
  • 11.
    Make the leapfrom SQL to MongoDB1. Map terms & concepts from SQL to MongoDB 2. Discover the 4 humongous advantages of MongoDB 3. Change your mindset in 3 key ways
  • 12.
    MongoDB stores datain documents
  • 13.
    MongoDB stores datain documents { first_name: "Paul", surname: "Miller", cell: "447557505611", city: "London", location: [45.123,47.232], profession: ["banking", "finance", "trader"], cars: [ { model: "Bentley", year: 1973 }, { model: "Rolls Royce", year: 1965 } ] } #MDBlocal @Lauren_Schaef
  • 14.
    MongoDB stores datain documents { first_name: "Paul", surname: "Miller", cell: "447557505611", city: "London", location: [45.123,47.232], profession: ["banking", "finance", "trader"], cars: [ { model: "Bentley", year: 1973 }, { model: "Rolls Royce", year: 1965 } ] } #MDBlocal @Lauren_Schaef
  • 15.
    MongoDB stores datain documents { first_name: "Paul", surname: "Miller", cell: "447557505611", city: "London", location: [45.123,47.232], profession: ["banking", "finance", "trader"], cars: [ { model: "Bentley", year: 1973 }, { model: "Rolls Royce", year: 1965 } ] } #MDBlocal @Lauren_Schaef
  • 16.
    MongoDB stores datain documents { first_name: "Paul", surname: "Miller", cell: "447557505611", city: "London", location: [45.123,47.232], profession: ["banking", "finance", "trader"], cars: [ { model: "Bentley", year: 1973 }, { model: "Rolls Royce", year: 1965 } ] } #MDBlocal @Lauren_Schaef
  • 17.
    MongoDB stores datain documents { first_name: "Paul", surname: "Miller", cell: "447557505611", city: "London", location: [45.123,47.232], profession: ["banking", "finance", "trader"], cars: [ { model: "Bentley", year: 1973 }, { model: "Rolls Royce", year: 1965 } ] } #MDBlocal @Lauren_Schaef
  • 18.
    MongoDB stores datain documents { first_name: "Paul", surname: "Miller", cell: "447557505611", city: "London", location: [45.123,47.232], profession: ["banking", "finance", "trader"], cars: [ { model: "Bentley", year: 1973 }, { model: "Rolls Royce", year: 1965 } ] } #MDBlocal @Lauren_Schaef
  • 19.
    MongoDB stores datain documents { first_name: "Paul", surname: "Miller", cell: "447557505611", city: "London", location: [45.123,47.232], profession: ["banking", "finance", "trader"], cars: [ { model: "Bentley", year: 1973 }, { model: "Rolls Royce", year: 1965 } ] } #MDBlocal @Lauren_Schaef
  • 20.
    MongoDB stores datain documents { first_name: "Paul", surname: "Miller", cell: "447557505611", city: "London", location: [45.123,47.232], profession: ["banking", "finance", "trader"], cars: [ { model: "Bentley", year: 1973 }, { model: "Rolls Royce", year: 1965 } ] } #MDBlocal @Lauren_Schaef
  • 21.
    Modeling data inMongoDB vs SQL { first_name: "Paul", surname: "Miller", cell: "447557505611", city: "London", location: [45.123,47.232], profession: ["banking", "finance", "trader"], cars: [ { model: "Bentley", year: 1973 }, { model: "Rolls Royce", year: 1965 } ] } #MDBlocal @Lauren_Schaef
  • 22.
    Modeling data inMongoDB vs SQL { first_name: "Paul", surname: "Miller", cell: "447557505611", city: "London", location: [45.123,47.232], profession: ["banking", "finance", "trader"], cars: [ { model: "Bentley", year: 1973 }, { model: "Rolls Royce", year: 1965 } ] } ID first_name surname cell city location_x location_y 1 Paul Miller 447557505611 London 45.123 47.232 Users #MDBlocal @Lauren_Schaef
  • 23.
    Modeling data inMongoDB vs SQL { first_name: "Paul", surname: "Miller", cell: "447557505611", city: "London", location: [45.123,47.232], profession: ["banking", "finance", "trader"], cars: [ { model: "Bentley", year: 1973 }, { model: "Rolls Royce", year: 1965 } ] } ID first_name surname cell city location_x location_y 1 Paul Miller 447557505611 London 45.123 47.232 Users ID user_id profession 10 1 banking 11 1 finance 12 1 trader Professions #MDBlocal @Lauren_Schaef
  • 24.
    Modeling data inMongoDB vs SQL { first_name: "Paul", surname: "Miller", cell: "447557505611", city: "London", location: [45.123,47.232], profession: ["banking", "finance", "trader"], cars: [ { model: "Bentley", year: 1973 }, { model: "Rolls Royce", year: 1965 } ] } ID user_id profession 10 1 banking 11 1 finance 12 1 trader Professions ID user_id model year 20 1 Bentley 1973 21 1 Rolls Royce 1965 Cars ID first_name surname cell city location_x location_y 1 Paul Miller 447557505611 London 45.123 47.232 Users #MDBlocal @Lauren_Schaef
  • 25.
    Modeling data inMongoDB vs SQL { first_name: "Paul", surname: "Miller", cell: "447557505611", city: "London", location: [45.123,47.232], profession: ["banking", "finance", "trader"], cars: [ { model: "Bentley", year: 1973 }, { model: "Rolls Royce", year: 1965 } ] } ID user_id profession 10 1 banking 11 1 finance 12 1 trader Professions ID user_id model year 20 1 Bentley 1973 21 1 Rolls Royce 1965 Cars ID first_name surname cell city location_x location_y 1 Paul Miller 447557505611 London 45.123 47.232 Users #MDBlocal @Lauren_Schaef
  • 26.
    Collections vs Tables { first_name:"Paul", surname: "Miller", cell: "447557505611", city: "London", location: [45.123,47.232], profession: ["banking", "finance", "trader"], cars: [ { model: "Bentley", year: 1973 }, { model: "Rolls Royce", year: 1965 } ] } { first_name: ”Lauren", surname: ”Schaefer", cell: ”1235552222", city: ”Lancaster", profession: [”software engineer", ”developer advocate"], } { first_name: ”Sydney", surname: ”Schaefer", city: ”Lancaster", school: ”Daisy’s Daycare” } ID first_name surname cell city location_x location_y 1 Paul Miller 447557505611 London 45.123 47.232 2 Lauren Schaefer 1235552222 Lancaster NULL NULL 3 Sydney Schaefer NULL Lancaster NULL NULL UsersUsers #MDBlocal @Lauren_Schaef
  • 27.
    ID first_name surnamecell city location_x location_y 1 Paul Miller 447557505611 London 45.123 47.232 2 Lauren Schaefer 1235552222 Lancaster NULL NULL 3 Sydney Schaefer NULL Lancaster NULL NULL Collections vs Tables { first_name: "Paul", surname: "Miller", cell: "447557505611", city: "London", location: [45.123,47.232], profession: ["banking", "finance", "trader"], cars: [ { model: "Bentley", year: 1973 }, { model: "Rolls Royce", year: 1965 } ] } { first_name: ”Lauren", surname: ”Schaefer", cell: ”1235552222", city: ”Lancaster", profession: [”software engineer", ”developer advocate"], } { first_name: ”Sydney", surname: ”Schaefer", city: ”Lancaster", school: ”Daisy’s Daycare” } UsersUsers #MDBlocal @Lauren_Schaef
  • 28.
    Collections vs Tables IDfirst_name surname cell city location_x location_y 1 Paul Miller 447557505611 London 45.123 47.232 2 Lauren Schaefer 1235552222 Lancaster NULL NULL 3 Sydney Schaefer NULL Lancaster NULL NULL { first_name: "Paul", surname: "Miller", cell: "447557505611", city: "London", location: [45.123,47.232], profession: ["banking", "finance", "trader"], cars: [ { model: "Bentley", year: 1973 }, { model: "Rolls Royce", year: 1965 } ] } { first_name: ”Lauren", surname: ”Schaefer", cell: ”1235552222", city: ”Lancaster", profession: [”software engineer", ”developer advocate"], } { first_name: ”Sydney", surname: ”Schaefer", city: ”Lancaster", school: ”Daisy’s Daycare” } UsersUsers #MDBlocal @Lauren_Schaef
  • 29.
  • 30.
    Schemaless database Don’t panic! Use schemavalidation. #MDBlocal @Lauren_Schaef
  • 31.
    Document Row { ... a: “b” ... } IDa ... 1 b ... 2 ... ... 3 ... ...
  • 32.
    Document Row(s) { ... a: “b” ... } IDa ... 1 b ... 2 ... ... 3 ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
  • 33.
    Field Column ID a... 1 b ... 2 c ... 3 ... ... { ... a: “b” ... } { ... a: “c” ... }
  • 34.
    Collection Table { ... } ... ...... ... ... ... ... ... ... ... ... ... { ... } { ... }
  • 35.
    Database Database ... ...... ... ... ... ... ... ... ... ... ... { ... } { ... } { ... } { ... } { ... } { ... } { ... } ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
  • 36.
    Index Index { ... } { ... } { ... } { ... } ... ...... ... ... ... ... ... ... ... ... ... ... ... ...
  • 37.
    View View { ... } ... ...... ... ... ... ... ... ... ... ... ... { ... } { ... }
  • 38.
    Embedding Join { ... a: “b”, ... c:{ d: “e” ... }, ... } ID a ... 1 b ... 2 ... ... 3 ... ... ... d ... 1 e ... ... ... ...
  • 39.
    Database References Join ID... ... 1 ... ... 2 ... ... 3 ... ... ... ... ... 1 ... ... ... ... ... { ... } { ... } { ... } { ... } { ... } { ... } { ... }
  • 40.
    $lookup (Aggregation Pipeline) Left OuterJoin ID ... ... 1 ... ... 2 ... ... 3 ... ... ... ... ... 1 ... ... 4 ... ... { ... } { ... } { ... } { ... } { ... } { ... } { ... }
  • 41.
    $graphLookup (Aggregation Pipeline) Recursive Common TableExpressions { ... } ... ... ... ... ... ... ... ... ... ... ... ... { ... } { ... }
  • 42.
  • 43.
    Term mapping summary x RowColumn Table Database Index Join Join Left Outer Join Recursive Common Table Expressions View Transaction Document Field Collection Database Index Embedding Database References $lookup $graphLookup View Transaction #MDBlocal @Lauren_Schaef
  • 44.
    Make the leapfrom SQL to MongoDB 1. Map terms & concepts from SQL to MongoDB 2. Discover the 4 humongous advantages of MongoDB 3. Change your mindset in 3 key ways #MDBlocal @Lauren_Schaef
  • 47.
    Scale cheaper! As thesize of your database grows, scale horizontally. #MDBlocal @Lauren_Schaef
  • 48.
    SQLMongoDB Query faster! Stop doingexpensive joins to get your data. ID a ... 1 b ... 2 ... ... 3 ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... { ... a: “b”, ... c: { d: “e” ... }, ... } #MDBlocal @Lauren_Schaef
  • 49.
    SQLMongoDB Pivot easier! Easily changethe shape of your data as your app evolves. { a: “b”, c: “one”, e: “f” } { a: “b”, c: 1, new: “no biggee” } ALTER TABLE `mydb`.`letters_table` DROP COLUMN `e`, ADD COLUMN `New` VARCHAR(45) NULL AFTER `C`, CHANGE COLUMN `C` `C` INT NULL DEFAULT NULL ; #MDBlocal @Lauren_Schaef
  • 50.
    Program faster! Documents mapto data structures in most popular languages. Update Your Profile #MDBlocal @Lauren_Schaef
  • 51.
    Program faster! { first_name: "Paul", surname:"Miller", cell: "447557505611", city: "London", location: [45.123,47.232], profession: ["banking", "finance", "trader"], } ID first_name surname cell city location_x location_y 1 Paul Miller 447557505611 London 45.123 47.232 Users ID user_id profession 10 1 banking 11 1 finance 12 1 trader Professions #MDBlocal @Lauren_Schaef
  • 52.
    Program faster! import pymongo frompymongo import MongoClient # CONNECT TO THE DB client = MongoClient() client = pymongo.MongoClient("mongodb+srv:// username:password@cluster0nsdia. mongodb.net/test?retryWrites=true") db = client.fabapp # THE ID OF THE USER WHOSE PROFILE WE # WILL BE RETRIEVING AND UPDATING userId = 1 import mysql.connector # CONNECT TO THE DB mydb = mysql.connector.connect( host="localhost", user=”username", passwd=”password", database=”fabapp” ) mycursor = mydb.cursor() # THE ID OF THE USER WHOSE PROFILE WE # WILL BE RETRIEVING AND UPDATING userId = 1
  • 53.
    Program faster! # GETTHE USER'S PROFILE INFORMATION ## We can pull all of the info from ## the same document since we used ## embedding user = db['Users'].find_one({"_id":userId}) # GET THE USER'S PROFILE INFORMATION ### Pull the info from the Users table ### & put it in the user dictionary sql = "Select * FROM Users WHERE Users.ID=%s” values = (userId,) mycursor.execute(sql, values) result = mycursor.fetchone() user = { "first_name": result[1], "surname": result[2], "cell": result[3], "city": result[4], "location_x": result[5], "location_y": result[6] } ### Pull the info from the Professions ### table & put it in the user dict. sql = "Select * FROM Professions WHERE Professions.user_id=%s” values = (userId,) mycursor.execute(sql, values) results = mycursor.fetchall() professions = [] for result in results: professions.append(result[2]) user["professions"] = professions
  • 54.
    Program faster! # UPDATETHE USER DICTIONARY BASED ON # USER INPUT IN THE APP ### We'll update the user dictionary ### manually for simplicity user = { "first_name": "NewFirst", "surname": "NewSurname", "cell": "123-456-7890", "city": "NewCity", "location": [40.762, -73.979], "professions": ["Manager", "Engineer"] } # UPDATE THE USER DICTIONARY BASED ON # USER INPUT IN THE APP ### We'll update the user dictionary ### manually for simplicity user = { "first_name": "NewFirst", "surname": "NewSurname", "cell": "123-456-7890", "city": "NewCity", "location_x": 40.762, "location_y": 73.979, "professions": ["Manager", "Engineer"] }
  • 55.
    Program faster! # UPDATETHE USER'S PROFILE IN THE DB ### Since the user's data is stored in ### a single document, we only have to ### make one update result = db['Users'].update_one( {"_id": userId}, {"$set": user}) # UPDATE THE USER'S PROFILE IN THE DB ### First update what is stored in the ### Users table sql = "UPDATE Users SET first_name=%s, surname=%s, cell=%s, city=%s, location_x=%s, location_y=%s WHERE (ID=%s)" values = ( user["first_name"], user["surname"], user["cell"], user["city"], user["location_x"], user["location_y"], userId) mycursor.execute(sql, values) mydb.commit() ### Delete existing records in ### Professions table and add new ones sql = "DELETE FROM Professions WHERE user_id=%s” values = (userId,) mycursor.execute(sql, values) mydb.commit() if(len(user["professions"]) > 0): sql = "INSERT INTO Professions (user_id,profession) VALUES (%s, %s)” values = [] for profession in user["professions"]: values.append((userId, profession)) mycursor.executemany(sql,values) mydb.commit()
  • 56.
    Program faster! 30 linesof code 73 lines of code
  • 57.
    Make the leapfrom SQL to MongoDB 1. Map terms & concepts from SQL to MongoDB 2. Discover the 4 humongous advantages of MongoDB 3. Change your mindset in 3 key ways #MDBlocal @Lauren_Schaef
  • 60.
  • 61.
    Embrace document diversity ThePolymorphic Pattern { first_name: "Paul", surname: "Miller", cell: "447557505611", city: "London", location: [45.123,47.232], profession: ["banking", "finance", "trader"], cars: [ { model: "Bentley", year: 1973 }, { model: "Rolls Royce", year: 1965 } ] } { first_name: ”Lauren", surname: ”Schaefer", cell: ”1235552222", city: ”Lancaster", profession: [”software engineer", ”developer advocate"], } { first_name: ”Sydney", surname: ”Schaefer", city: ”Lancaster", school: ”Daisy’s Daycare” } #MDBlocal @Lauren_Schaef
  • 62.
    Embrace document diversity TheOutlier Pattern { _id: ”Lauren_Schaefer", displayName: ”Lauren Schaefer", numFollowers: 1310 followers: [ “naomi_pen”, “kenwalger”, “mylynn” ... ] } { _id: ”Nick_Offerman", displayName: ”Nick Offerman", numFollowers: 1730332 followers: [ “c_hotaling”, “IAmJerdog”, “ChloeCondon” ... ], has_extras: true } { _id: ”Nick_Offerman_1", twitter_id: “Nick_Offerman”, is_overflow: true, followers: [ “StephenAtHome”, “TheEllenShow”, “hulu” ... ] } #MDBlocal @Lauren_Schaef
  • 63.
    Data that isaccessed together should be stored together #MDBlocal @Lauren_Schaef
  • 64.
    Data that isaccessed together should be stored together #MDBlocal @Lauren_Schaef
  • 65.
    0 20000 40000 60000 80000 100000 120000 1985 2017 Storage vsDeveloper Costs Storage Cost per GB Developer Salary Data that is accessed together should be stored together #MDBlocal @Lauren_Schaef
  • 66.
    Don’t normalize yourdata for the sake of normalizing it. { a: “b”, c: { d: “e” ... }, f: [“g”, “h”, “i”], j: [ { k: “l” }, { m: “n” } ] } Data that is accessed together should be stored together #MDBlocal @Lauren_Schaef
  • 67.
    Tread carefully withtransactions Relying on transactions is a bad design smell. { ... } { ... } { ... } { ... } { ... } { ... } { ... } #MDBlocal @Lauren_Schaef
  • 68.
    Make the leapfrom SQL to MongoDB #MDBlocal @Lauren_Schaef
  • 69.
    Make the leapfrom SQL to MongoDB 1. Map terms & concepts from SQL to MongoDB x Row Column Table Database Index Join Join Left Outer Join Recursive Common Table Expressions View Transaction Document Field Collection Database Index Embedding Database References $lookup $graphLookup View Transaction #MDBlocal @Lauren_Schaef
  • 70.
    Make the leapfrom SQL to MongoDB 1. Map terms & concepts from SQL to MongoDB 2. Discover the 4 humongous advantages of MongoDB #MDBlocal @Lauren_Schaef
  • 71.
    Make the leapfrom SQL to MongoDB1. Map terms & concepts from SQL to MongoDB 2. Discover the 4 humongous advantages of MongoDB 1. Scale cheaper #MDBlocal @Lauren_Schaef
  • 72.
    Make the leapfrom SQL to MongoDB 1. Map terms & concepts from SQL to MongoDB 2. Discover the 4 humongous advantages of MongoDB 1. Scale cheaper 2. Query faster #MDBlocal @Lauren_Schaef
  • 73.
    Make the leapfrom SQL to MongoDB 1. Map terms & concepts from SQL to MongoDB 2. Discover the 4 humongous advantages of MongoDB 1. Scale cheaper 2. Query faster 3. Pivot easier #MDBlocal @Lauren_Schaef
  • 74.
    Make the leapfrom SQL to MongoDB 1. Map terms & concepts from SQL to MongoDB 2. Discover the 4 humongous advantages of MongoDB 1. Scale cheaper 2. Query faster 3. Pivot easier 4. Program faster # GET THE USER'S PROFILE INFORMATION ## We can pull all of the info from ## the same document since we used ## embedding user = db['Users'].find_one({"_id":userId}) # GET THE USER'S PROFILE INFORMATION ### Pull the info from the Users table ### & put it in the user dictionary sql = "Select * FROM Users WHERE Users.ID=%s” values = (userId,) mycursor.execute(sql, values) result = mycursor.fetchone() user = { "first_name": result[1], "surname": result[2], "cell": result[3], "city": result[4], "location_x": result[5], "location_y": result[6] } #MDBlocal @Lauren_Schaef
  • 75.
    Make the leapfrom SQL to MongoDB 1. Map terms & concepts from SQL to MongoDB 2. Discover the 4 humongous advantages of MongoDB 3. Change your mindset in 3 key ways #MDBlocal @Lauren_Schaef
  • 76.
    Make the leapfrom SQL to MongoDB 1. Map terms & concepts from SQL to MongoDB 2. Discover the 4 humongous advantages of MongoDB 3. Change your mindset in 3 key ways 1. Embrace document diversity #MDBlocal @Lauren_Schaef
  • 77.
    Make the leapfrom SQL to MongoDB1. Map terms & concepts from SQL to MongoDB 2. Discover the 4 humongous advantages of MongoDB 3. Change your mindset in 3 key ways 1. Embrace document diversity 2. Data that is accessed together should be stored together Make the leap from SQL to MongoDB 1. Map terms & concepts from SQL to MongoDB 2. Discover the 4 humongous advantages of MongoDB 3. Change your mindset in 3 key ways 1. Embrace document diversity 2. Data that is accessed together should be stored together #MDBlocal @Lauren_Schaef
  • 78.
    Make the leapfrom SQL to MongoDB 1. Map terms & concepts from SQL to MongoDB 2. Discover the 4 humongous advantages of MongoDB 3. Change your mindset in 3 key ways 1. Embrace document diversity 2. Data that is accessed together should be stored together 3. Tread carefully with transactions #MDBlocal @Lauren_Schaef
  • 79.
    Don’t be RonSwanson (in this particular case) #MDBlocal @Lauren_Schaef
  • 80.
    Change your mindset& get the full value of MongoDB Don’t be Ron Swanson #MDBlocal @Lauren_Schaef
  • 81.
    Additional resources ondata modeling patterns • Advanced Schema Design Patterns (webinar) • Building with Patterns: A Summary (blog series) • M320: Data Modeling (MongoDB University Course – brand new!) #MDBlocal @Lauren_Schaef
  • 82.
    Additional resources • TheMongoDB Docs • JSON Schema Validation – Locking down your model the smart way • JSON Schema Validation - Checking Your Arrays • M121: The MongoDB Aggregation Framework #MDBlocal @Lauren_Schaef
  • 83.
    Don’t be RonSwanson (in this particular case) Change your mindset and get the full value of MongoDB Change your mindset & get the full value of MongoDB Get the slides on my Twitter page: @Lauren_Schaefer #MDBlocal @Lauren_Schaef
  • 84.

Editor's Notes

  • #4 Parks N Rec Season 6, episode 14
  • #5 http://www.breathtakingandinappropriate.com/2014/09/ron-swanson-has-no-time-for-frozen.html
  • #6 http://www.breathtakingandinappropriate.com/2014/09/ron-swanson-has-no-time-for-frozen.html
  • #7 http://www.breathtakingandinappropriate.com/2014/09/ron-swanson-has-no-time-for-frozen.html
  • #8 3 problems Snail mail is way slower than posting the review to yelp where it will be instantly available The business he’s reviewing may never open the review No one else will benefit from the review
  • #10 Assumptions: You have experience with SQL databases Minimal to no experience with MongoDB Today we’re going to be focused on going from SQL to MongoDB
  • #14 JavaScript Object Notation MongoDB’s document model is not just a key-value store.
  • #15 Fields
  • #16 Field values
  • #17 Field values Strings
  • #18 32-bit integer Double, longs, and decimals
  • #19 Geo-location.
  • #20 Fields can contain arrays. Here we see an array of Strings
  • #21 Fields can contain an array of subdocuments The value of a field can be a variety of types including objects, booleans, dates, and timestamps among others.
  • #22 When people talk about MongoDB, they’ll often use the term nonrelational. But that doesn’t mean we don’t store relationships. We just do it in a different way. Let’s walk through an example of how you would model this same data we have in the document on the left in SQL tables
  • #27 We store documents together in Collections. Collections roughly map to SQL tables.
  • #28 Not all documents have to have the same shape. We use the term polymorphic for this. As you can see, the Lauren document doesn’t have fields for location or cars, which is completely ok. We simply omit the fields from the document. In the SQL world, all rows in a table must have the same fields. Since we don’t have location data for Lauren, we have to use NULL values, which is typically discouraged.
  • #29 Now let’s take a look at the Sydney document. In this case, Sydney is a kid. She doesn’t have a lot of the data that Paul did. She’s missing a cell phone, location, profession, and car data. If we take a look at the Users table on the right, we can see that she has NULL values in her row just like Lauren did. Since Sydney is a kid, she has an extra field that neither the Paul document nor the Lauren document had. She has a school field. When you’re using documents, this is not a problem. You can simply add the field as we did here. Now when we look at the SQL table on the right, things get a bit more complicated. What do we have to do to store this data? We need to convince our DBA to add the field, take our database down, add the school column, add NULL values for every row that doesn’t have a value for school, and bring the database back up. It’s a bit messier.
  • #30 http://gph.is/28MrIOY
  • #31 http://gph.is/28MrIOY
  • #39 There are a few different ways to handle joins in MongoDB. In general, we recommend just embedding the information in a subdocument or an array that you would put in a separate tabled. The rule of thumb is that data that is accessed together should be stored together. So, if you’ll be accessing the information that you would have put in separate tables together most of the time, you should likely just embed it.
  • #40 As I just said, for many use cases, embedding in a single document is optimal. In some cases, it makes sense to store related information in separate documents, typically in different collections or databases. I’m not going to get into the details here of how to do this, but basically, you can create a reference from one document to another—very similar to how you would use foreign keys in SQL.
  • #41 Another option is to use $lookup. You can use $lookup when you are using the Aggregation Pipeline. $lookup is roughly equivalent to a left outer join. Again, I’m not going to get into the details of how $lookup works, but I want you to be aware that $lookup exists. Caveats Unsharded collection in the same database
  • #42 Model an org chart
  • #43 In MongoDB 4.0, transactions work across a replica set. Check out the keynote tomorrow for some exciting announcements around transactions. (MongoDB 4.2 will extend support to transactions across a sharded deployment*)
  • #45 No particular order
  • #46 http://gph.is/1ZTM9ct 4 advantages in no particular order
  • #47 http://gph.is/1ZTM9ct 4 advantages in no particular order
  • #49 Data that is accessed together should be stored together
  • #50 Alter Table (maybe take a couple of hours). May need to schedule downtime. May kill the performance of your app while the table is being altered.
  • #54 Documents map to data structures in most popular languages. Chose to make 2 separate queries. Could have done a join here, but then we ended up with 3 rows with a lot of duplicate data.
  • #56 Fewer lines of code and fewer opportunities for bugs and that’s because documents map to data structures in most popular programming languages
  • #58 No particular order
  • #59 People pick up MongoDB and try to use it as a relational DB are the ones who fail and struggle. You can’t keep doing things in the same way. http://gph.is/XK6p3t Be explicit these are 3 things in no particular order.
  • #60 People pick up MongoDB and try to use it as a relational DB are the ones who fail and struggle. You can’t keep doing things in the same way. http://gph.is/XK6p3t Be explicit these are 3 things in no particular order.
  • #62 When all documents in a collection are of similar but not identical structure, we call this the Polymorphic Pattern. You use this pattern when you want to be able to query across information that is similar in nature from a single collection.
  • #63 Let’s say we are modeling Twitter data. Let’s say we want to display the Twitter user’s name, number of followers, and a list of their followers. In most cases, modeling your data something like this would work: As you can see, we’re storing the Twitter handle as the id, my display name, my number of followers, and a list of my followers. But what about really popular Twitter users who have millions of followers? Listing all of their their Twitter followers in the document may not be practical due to the 16MB document size limit. When you have millions of followers, you probably need to list them in a separate document or two. So, do we want to model our data for these rare, outlier cases? Or do we want to model our data for the typical use case. We want to model our data for the typical use case so we can optimize for it. So, we could model my Twitter information just like you see here. Now let’s look at a popular Twitter user like Nick Offerman who has millions of followers. The document for his Twitter data would be very similar to mine…he has an id, displayName, and numFollowers. Since all of his followers won’t fit in the followers array, we’ll add an additional field to his document called has_extras. Then we’ll create a new document. We’ll use the twitter_id field to reference the id of Nick’s original document. We’ll create an is_overflow field to indicate this is an overflow document. And then we can begin listing followers. If we determine we have too many followers to store in this document, we could create another overflow document. The great thing about the Outlier Pattern is that we are optimizing for the typical use case, but we have the flexibility to handle outliers.
  • #64 We’ve all heard it over and over, you should normalize your data. But why? When relational databases became popular, disk space was extremely expensive. Financially, it made sense to normalize data and save disk space. As you can see from this chart and you know from buying flash drives, the price of storage has dramatically decreased. Our phones, tablets, laptops, and flash drives have more storage capacity today than they did even 5-10 years ago for a a fraction of the cost.
  • #65 We’ve all heard it over and over, you should normalize your data. But why? When relational databases became popular, disk space was extremely expensive. Financially, it made sense to normalize data and save disk space. As you can see from this chart and you know from buying flash drives, the price of storage has dramatically decreased. Our phones, tablets, laptops, and flash drives have more storage capacity today than they did even 5-10 years ago for a a fraction of the cost.
  • #66 The costs for application development have shifted from physical resources, such as RAM and disk storage, being the largest cost to developers. 30-40 years ago, we optimized applications for storage due to costs. With developer cost being a major factor in the development cycle, there’s a lot of pressure to optimize for developer time. We want, and even need, developers to be agile in their development, iterate quickly, and concentrate on business application logic and not on data storage specifics. Just think back to the example I gave you earlier of how much faster and easier it is to program with MongoDB verses SQL. As a developer, that’s what I want. I’d much rather be focusing on implementing my app than being dragged down by data storage specifics. How many people are developers or programmers or DBAs? How many people here are managers of developers or programmers or DBAs?
  • #67 Leverage embedding. Consider how you can use subdocuments, arrays, and arrays of subdocuments to store your data together. Resist the temptation to break up your data. Data that is accessed together should be stored together. If you end up repeating data in your database, that’s ok—especially if you won’t be updating it very often.
  • #68 Don’t fall into the trap of thinking MongoDB doesn’t support ACID.  It does. But, if you’re using transactions regularly, you probably need to restructure your data. Explain how this builds on the first two Not all documents need to have the same fields Data that is accessed together should be stored together
  • #69 No particular order
  • #73 Data that is accessed together should be stored together.
  • #75 Documents map to data structures in most popular languages.
  • #80 http://gph.is/XK6p3t
  • #81 https://giphy.com/gifs/dancing-parks-and-recreation-ron-swanson-iOz3p2txHIo4U
  • #84 https://giphy.com/gifs/dancing-parks-and-recreation-ron-swanson-iOz3p2txHIo4U