SlideShare a Scribd company logo
Jumpstart! From SQL to NoSQL -- Changing Your Mindset
From SQL to NoSQL—
Changing Your Mindset
@Lauren_Schaefer, Developer Advocate,
MongoDB
Parks and Recreation, Season 6, Episode 14
Jumpstart! From SQL to NoSQL -- Changing Your Mindset
Jumpstart! From SQL to NoSQL -- Changing Your Mindset
Jumpstart! From SQL to NoSQL -- Changing Your Mindset
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
Jumpstart! From SQL to NoSQL -- Changing Your Mindset
Jumpstart! From SQL to NoSQL -- Changing Your Mindset
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
Jumpstart! From SQL to NoSQL -- Changing Your Mindset
Jumpstart! From SQL to NoSQL -- Changing Your Mindset
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

More Related Content

PPTX
CQRS recipes or how to cook your architecture
PDF
GraphX and Pregel - Apache Spark
PPTX
Mongodb vs mysql
PPTX
Agile Data Mining with Data Vault 2.0 (english)
PDF
Amsterdam - The Neo4j Graph Data Platform Today & Tomorrow
PPTX
Data quality + data governance: the formula for bigger, better decisions
PPTX
MongoDB presentation
PDF
Apache Cassandra and DataStax Enterprise Explained with Peter Halliday at Wil...
CQRS recipes or how to cook your architecture
GraphX and Pregel - Apache Spark
Mongodb vs mysql
Agile Data Mining with Data Vault 2.0 (english)
Amsterdam - The Neo4j Graph Data Platform Today & Tomorrow
Data quality + data governance: the formula for bigger, better decisions
MongoDB presentation
Apache Cassandra and DataStax Enterprise Explained with Peter Halliday at Wil...

What's hot (20)

PPTX
Introduction to NOSQL databases
PDF
PPTX
The Basics of MongoDB
PDF
HandsOn ProxySQL Tutorial - PLSC18
PDF
Webinar: Working with Graph Data in MongoDB
PPTX
data-mesh-101.pptx
ODP
Introduction to MongoDB
PDF
The perfect couple: Uniting Large Language Models and Knowledge Graphs for En...
PPTX
MongoDB
PDF
Introduction to Cassandra
PDF
MariaDB 10: The Complete Tutorial
PDF
Pourquoi Leroy Merlin a besoin d'un Knowledge Graph ?
PDF
The Modern Data Team for the Modern Data Stack: dbt and the Role of the Analy...
PDF
Knowledge Graphs and Graph Data Science: More Context, Better Predictions (Ne...
PDF
Get to know PostgreSQL!
PPTX
PostGreSQL Performance Tuning
PPTX
03 spark rdd operations
PPTX
What is gRPC introduction gRPC Explained
PDF
Log analysis with elastic stack
PDF
Speeding Time to Insight with a Modern ELT Approach
Introduction to NOSQL databases
The Basics of MongoDB
HandsOn ProxySQL Tutorial - PLSC18
Webinar: Working with Graph Data in MongoDB
data-mesh-101.pptx
Introduction to MongoDB
The perfect couple: Uniting Large Language Models and Knowledge Graphs for En...
MongoDB
Introduction to Cassandra
MariaDB 10: The Complete Tutorial
Pourquoi Leroy Merlin a besoin d'un Knowledge Graph ?
The Modern Data Team for the Modern Data Stack: dbt and the Role of the Analy...
Knowledge Graphs and Graph Data Science: More Context, Better Predictions (Ne...
Get to know PostgreSQL!
PostGreSQL Performance Tuning
03 spark rdd operations
What is gRPC introduction gRPC Explained
Log analysis with elastic stack
Speeding Time to Insight with a Modern ELT Approach
Ad

Similar to Jumpstart! From SQL to NoSQL -- Changing Your Mindset (20)

PDF
MongoDB World 2019: From SQL to NoSQL -- Changing Your Mindset
PDF
From SQL to NoSQL -- Changing Your Mindset
PDF
MongoDB .local Houston 2019: Jumpstart: From SQL to NoSQL -- Changing Your Mi...
PDF
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
PDF
MongoDB .local Chicago 2019: From SQL to NoSQL -- Changing Your Mindset
PPTX
Intro to MongoDB Workshop
PPTX
Intro to MongoDB (Extended Session)
PDF
MongoDB: Back to Basics
PDF
Jumpstart! Building Your First MongoDB App Using Atlas & Stitch
PPTX
Jumpstart: Introduction to Schema Design
PDF
MongoDB .local Chicago 2019: Practical Data Modeling for MongoDB: Tutorial
KEY
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
PDF
Which Questions We Should Have
PPTX
The Right (and Wrong) Use Cases for MongoDB
PDF
Single View of the Customer
PPTX
MongoDB Schema Design
PPTX
Creating a Single View: Overview and Analysis
KEY
mongoDB at Visibiz
PDF
Data Modeling and Relational to NoSQL
PPTX
MongoDB Knowledge share
MongoDB World 2019: From SQL to NoSQL -- Changing Your Mindset
From SQL to NoSQL -- Changing Your Mindset
MongoDB .local Houston 2019: Jumpstart: From SQL to NoSQL -- Changing Your Mi...
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local Chicago 2019: From SQL to NoSQL -- Changing Your Mindset
Intro to MongoDB Workshop
Intro to MongoDB (Extended Session)
MongoDB: Back to Basics
Jumpstart! Building Your First MongoDB App Using Atlas & Stitch
Jumpstart: Introduction to Schema Design
MongoDB .local Chicago 2019: Practical Data Modeling for MongoDB: Tutorial
NOSQL101, Or: How I Learned To Stop Worrying And Love The Mongo!
Which Questions We Should Have
The Right (and Wrong) Use Cases for MongoDB
Single View of the Customer
MongoDB Schema Design
Creating a Single View: Overview and Analysis
mongoDB at Visibiz
Data Modeling and Relational to NoSQL
MongoDB Knowledge share
Ad

More from Lauren Hayward Schaefer (20)

PDF
7 Ways to Build an API that Developers Will Hate
PDF
Developer Advocacy: A Career Path for Those With a Passion for Code, Communit...
PDF
10 Best Practices for Writing Documentation (For Those Who Would Rather Do An...
PDF
Intro to Technical Writing: Creating Content that Google and Readers will Love
PDF
Level Up Your Technical Career by Writing
PDF
How to Raise Your Profile Worksheet
PDF
5 Things I Learned While Modeling Data in MongoDB
PPTX
How to Raise Your Profile as a Developer (And Why You Should Bother!)
PDF
Building CI/CD Pipelines for MongoDB Realm Apps
PDF
From Tables to Documents—Changing Your Database Mindset
PDF
NoSQL for Startups
PDF
Back to Basics 2021
PDF
DevOps + MongoDB Realm Serverless Functions = 🤩
PDF
Stop! Don't make these mistakes in your document database!
PPTX
From Tables to Documents—Changing Your Database Mindset
PPTX
From Tables to Documents -- Changing Your Database Mindset
PDF
Making #RemoteWork Actually Work
PPTX
DevOps + MongoDB Serverless = 
PDF
Does remote work *really* work?
PDF
Look, Ma! No servers! Serverless application development with MongoDB Stitch
7 Ways to Build an API that Developers Will Hate
Developer Advocacy: A Career Path for Those With a Passion for Code, Communit...
10 Best Practices for Writing Documentation (For Those Who Would Rather Do An...
Intro to Technical Writing: Creating Content that Google and Readers will Love
Level Up Your Technical Career by Writing
How to Raise Your Profile Worksheet
5 Things I Learned While Modeling Data in MongoDB
How to Raise Your Profile as a Developer (And Why You Should Bother!)
Building CI/CD Pipelines for MongoDB Realm Apps
From Tables to Documents—Changing Your Database Mindset
NoSQL for Startups
Back to Basics 2021
DevOps + MongoDB Realm Serverless Functions = 🤩
Stop! Don't make these mistakes in your document database!
From Tables to Documents—Changing Your Database Mindset
From Tables to Documents -- Changing Your Database Mindset
Making #RemoteWork Actually Work
DevOps + MongoDB Serverless = 
Does remote work *really* work?
Look, Ma! No servers! Serverless application development with MongoDB Stitch

Recently uploaded (20)

PDF
Electronic commerce courselecture one. Pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Spectroscopy.pptx food analysis technology
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPT
Teaching material agriculture food technology
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Machine learning based COVID-19 study performance prediction
PDF
KodekX | Application Modernization Development
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
Electronic commerce courselecture one. Pdf
MIND Revenue Release Quarter 2 2025 Press Release
Unlocking AI with Model Context Protocol (MCP)
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
NewMind AI Weekly Chronicles - August'25 Week I
Reach Out and Touch Someone: Haptics and Empathic Computing
Spectroscopy.pptx food analysis technology
Advanced methodologies resolving dimensionality complications for autism neur...
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Teaching material agriculture food technology
Chapter 3 Spatial Domain Image Processing.pdf
Spectral efficient network and resource selection model in 5G networks
The Rise and Fall of 3GPP – Time for a Sabbatical?
Programs and apps: productivity, graphics, security and other tools
Machine learning based COVID-19 study performance prediction
KodekX | Application Modernization Development
Dropbox Q2 2025 Financial Results & Investor Presentation
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
“AI and Expert System Decision Support & Business Intelligence Systems”

Jumpstart! From SQL to NoSQL -- Changing Your Mindset

  • 2. From SQL to NoSQL— 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 to NoSQL— Changing Your Mindset
  • 10. 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
  • 11. 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
  • 12. MongoDB stores data in documents
  • 13. 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
  • 14. 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
  • 15. 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
  • 16. 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
  • 17. 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
  • 18. 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
  • 19. 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
  • 20. 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
  • 21. 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
  • 22. 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
  • 23. 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
  • 24. 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
  • 25. 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
  • 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 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
  • 28. 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
  • 30. Schemaless database Don’t panic! Use schema validation. #MDBlocal @Lauren_Schaef
  • 31. Document Row { ... a: “b” ... } ID a ... 1 b ... 2 ... ... 3 ... ...
  • 32. Document Row(s) { ... a: “b” ... } ID a ... 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 Outer Join ID ... ... 1 ... ... 2 ... ... 3 ... ... ... ... ... 1 ... ... 4 ... ... { ... } { ... } { ... } { ... } { ... } { ... } { ... }
  • 41. $graphLookup (Aggregation Pipeline) Recursive Common Table Expressions { ... } ... ... ... ... ... ... ... ... ... ... ... ... { ... } { ... }
  • 42. Multi-Document ACID Transaction Multi-Record ACID Transaction { ... } { ... } { ... } { ... } { ... } { ... } { ... } ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
  • 43. 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
  • 44. 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
  • 47. Scale cheaper! As the size of your database grows, scale horizontally. #MDBlocal @Lauren_Schaef
  • 48. 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
  • 49. 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
  • 50. Program faster! Documents map to 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 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
  • 53. 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
  • 54. 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"] }
  • 55. 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()
  • 56. Program faster! 30 lines of code 73 lines of code
  • 57. 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
  • 61. 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
  • 62. 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
  • 63. Data that is accessed together should be stored together #MDBlocal @Lauren_Schaef
  • 64. Data that is accessed together should be stored together #MDBlocal @Lauren_Schaef
  • 65. 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
  • 66. 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
  • 67. Tread carefully with transactions Relying on transactions is a bad design smell. { ... } { ... } { ... } { ... } { ... } { ... } { ... } #MDBlocal @Lauren_Schaef
  • 68. Make the leap from SQL to MongoDB #MDBlocal @Lauren_Schaef
  • 69. 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
  • 70. 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
  • 71. 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
  • 72. 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
  • 73. 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
  • 74. 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
  • 75. 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
  • 76. 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
  • 77. 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
  • 78. 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
  • 79. Don’t be Ron Swanson (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 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
  • 82. 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
  • 83. 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

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