The document describes Lauren Schaefer's presentation on changing from a relational database mindset to a document database mindset using MongoDB. It provides an example of mapping user data from tables to embedded documents, showing how additional user details like hobbies and job history can be embedded within a user document rather than stored across multiple tables. The presentation aims to help developers understand the key differences between relational and document databases.
9. Themental
journey from
tables to
documents
1. Map terms & concepts from tables to documents
2. Discover the 4 major advantages of documents
3. Change your mindset in 3 key ways
@Lauren_Schaefer #ndcoslo
10. Themental
journey from
tables to
documents
1. Map terms & concepts from tables to documents
2. Discover the 4 major advantages of documents
3. Change your mindset in 3 key ways
@Lauren_Schaefer #ndcoslo
53. Themental
journey from
tables to
documents
1. Map terms & concepts from tables to documents
2. Discover the 4 major advantages of documents
3. Change your mindset in 3 key ways
@Lauren_Schaefer #ndcoslo
60. {
"_id": 1,
"first_name": "Leslie",
"last_name": "Yepp",
"cell": "8125552344",
"city": "Pawnee",
"location": [ -86.536632, 39.170344 ],
"hobbies": [
"scrapbooking",
"eating waffles",
"working”
]
}
ID first_name last_name cell city latitude longitude school
1 Leslie Yepp 812555234
4
Pawnee 39.170344 -86.536632 NULL
UsersUsers
ID user_id hobby
10 1 scrapbooking
11 1 eating waffles
12 1 working
Hobbies
Programfaster!
@Lauren_Schaefer #ndcoslo
61. import mysql.connector
# CONNECT TO THE DB
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="rootroot",
database="CityHall" )
mycursor =
mydb.cursor(dictionary=True)
# THE ID OF THE USER WHOSE PROFILE WE
# WILL BE RETRIEVING AND UPDATING
userId = 1
import pymongo
from pymongo import MongoClient
# CONNECT TO THE DB
client = MongoClient()
client = pymongo
.MongoClient("mongodb+srv://root:rootro
ot@mycluster.mongodb.net/test?retryWrit
es=true&w=majority")
db = client.CityHall
# THE ID OF THE USER WHOSE PROFILE WE
# WILL BE RETRIEVING AND UPDATING
userId = 1
Programfaster!
62. sql = "SELECT * FROM Users LEFT JOIN
Hobbies ON Users.ID = Hobbies.user_id
WHERE Users.id=%s"
values = (userId,)
my cursor.execute(sql, values)
user = mycursor.fetchone()
user = db['Users’]
.find_one({"_id": userId})
Programfaster!
hobbies = []
if (user["hobby"]):
hobbies.append(user["hobby"])
del user["hobby"]
del user["ID"]
for result in mycursor:
hobbies.append(result["hobby"])
user["hobbies"] = hobbies
68. Themental
journey from
tables to
documents
1. Map terms & concepts from tables to documents
2. Discover the 4 major advantages of documents
3. Change your mindset in 3 key ways
@Lauren_Schaefer #ndcoslo
83. Themental
journey from
tables to
documents
1. Map terms & concepts from tables to documents
2. Discover the 4 major advantages of documents
1. Scale cheaper
@Lauren_Schaefer #ndcoslo
84. Themental
journey from
tables to
documents
1. Map terms & concepts from tables to documents
2. Discover the 4 major advantages of documents
1. Scale cheaper
2. Query faster
@Lauren_Schaefer #ndcoslo
85. Themental
journey from
tables to
documents
1. Map terms & concepts from tables to documents
2. Discover the 4 major advantages of documents
1. Scale cheaper
2. Query faster
3. Pivot easier
@Lauren_Schaefer #ndcoslo
86. Themental
journey from
tables to
documents
1. Map terms & concepts from tables to documents
2. Discover the 4 major advantages of documents
1. Scale cheaper
2. Query faster
3. Pivot easier
4. Program faster
@Lauren_Schaefer #ndcoslo
87. Themental
journey from
tables to
documents
1. Map terms & concepts from tables to documents
2. Discover the 4 major advantages of documents
3. Change your mindset in 3 key ways
@Lauren_Schaefer #ndcoslo
88. Themental
journey from
tables to
documents
1. Map terms & concepts from tables to documents
2. Discover the 4 major advantages of documents
3. Change your mindset in 3 key ways
1. Embrace document diversity
@Lauren_Schaefer #ndcoslo
89. Themental
journey from
tables to
documents
1. Map terms & concepts from tables to documents
2. Discover the 4 major advantages of documents
3. Change your mindset in 3 key ways
1. Embrace document diversity
2. Data that is accessed together should be stored together
{
a: “b”,
c: {
d: “e”
...
},
f: [“g”, “h”, “i”]
}
@Lauren_Schaefer #ndcoslo
90. Themental
journey from
tables to
documents
1. Map terms & concepts from tables to documents
2. Discover the 4 major advantages of documents
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
@Lauren_Schaefer #ndcoslo
93. Additional
Resources
• SQL to MongoDB Blog (blog series)
• Quick Start: MongoDB and Node.js (blog series)
• Advanced Schema Design Patterns (webinar)
• Building with Patterns: A Summary (blog series)
• M320: Data Modeling (MongoDB University Course)
• JSON Schema Validation – Locking down your model
the smart way (blog)
@Lauren_Schaefer #ndcoslo