When you need to model data, is your first instinct to start breaking it down into rows and columns? Mine used to be too. When you want to develop apps in a modern, agile way, NoSQL databases can be the best option. Come to this talk to learn how to take advantage of all that NoSQL databases have to offer and discover the benefits of changing your mindset from the legacy, tabular way of modeling data. We’ll compare and contrast the terms and concepts in SQL databases and MongoDB, explain the benefits of using MongoDB compared to SQL databases, and walk through data modeling basics so you feel confident as you begin using MongoDB.
From SQL to NoSQL: Mapping Concepts and Advantages
1. #MDBlocal
From SQL to NoSQL
Changing Your Mindset
SANFRANCISCO
Girish Dusane
Senior Solution Architect
2. #MDBLocal
The mental journey from SQL to MongoDB
• Map terms & concepts
from SQL to MongoDB
• Discover the 4 humongous
advantages of MongoDB
• Change your mindset in 3
key ways
34. #MDBLocal
Summary of term mapping
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
35. #MDBLocal
The mental journey from SQL to MongoDB
• Map terms & concepts
from SQL to MongoDB
• Discover the 4 humongous
advantages of MongoDB
• Change your mindset in 3
key ways
38. #MDBLocal
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
39. 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
Program faster!
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
40. # 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]
}
# 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})
Program faster!
### Pull the info from the Professions table
& put it in the user dictionary
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
41. # 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"]
}
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_x": 40.762,
"location_y": 73.979,
"professions": [”Manager", "Engineer"]
}
42. # UPDATE THE USER'S PROFILE IN THE DATABASE
### 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()
# UPDATE THE USER'S PROFILE IN THE DATABASE
### 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})
Program faster!
### 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()
43. 30 lines of code
Program faster!
73 lines of code
44. #MDBLocal
Query Faster
Stop doing expensive joins to get your data.
SQLMongoDB
ID a ...
1 b ...
2 ... ...
3 ... ...
... ... ...
... ... ...
... ... ...
... ... ...
... ... ...
... ... ...
{
...
a: “b”,
...
c: {
d: “e”
...
},
...
}
45. #MDBLocal
Pivot easier
Easily change the shape of your data as your app evolves.
SQLMongoDB
{
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 ;
46. #MDBLocal
Summary - 4 advantages of MongoDB
• Scale cheaper
• Program faster
• Query faster
• Pivot faster
47. #MDBLocal
The mental journey from SQL to MongoDB
• Map terms & concepts
from SQL to MongoDB
• Discover the 4 humongous
advantages of MongoDB
• Change your mindset in 3
key ways
53. #MDBLocal
Data that is accessed together is stored together
0
20000
40000
60000
80000
100000
120000
1985 2017
Storage vs Developer Costs
Storage Cost per GB Developer Salary
54. #MDBLocal
Data that is accessed together is stored together
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”
}
]
}
55. #MDBLocal
Stop using transactions regularly
Relying on transactions is a bad design smell
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
56. #MDBLocal
Summary – Changing your mindset
• Not all documents in a collection need to have the
same fields
• Data that is accessed together should be stored
together
• Stop using transactions regularly
58. #MDBLocal
The mental journey from SQL to MongoDB
Map terms & concepts from SQL to MongoDB
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
59. #MDBLocal
The mental journey from SQL to MongoDB
Map terms & concepts from SQL to MongoDB
Discover the 4 humongous advantages of MongoDB
60. #MDBLocal
The mental journey from SQL to MongoDB
Map terms & concepts from SQL to MongoDB
Discover the 4 humongous advantages of MongoDB
• Scale cheaper
61. #MDBLocal
Map terms & concepts from SQL to MongoDB
Discover the 4 humongous advantages of MongoDB
• Scale cheaper
• Program faster
The mental journey from SQL to MongoDB
# UPDATE THE USER'S PROFILE IN THE DATABASE
### 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)
# UPDATE THE USER'S PROFILE IN THE DATABASE
### 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})
62. #MDBLocal
Map terms & concepts from SQL to MongoDB
Discover the 4 humongous advantages of MongoDB
• Scale cheaper
• Program faster
• Query faster
The mental journey from SQL to MongoDB
63. #MDBLocal
Map terms & concepts from SQL to MongoDB
Discover the 4 humongous advantages of MongoDB
• Scale cheaper
• Program faster
• Query faster
• Pivot easier
The mental journey from SQL to MongoDB
64. #MDBLocal
The mental journey from SQL to MongoDB
Map terms & concepts from SQL to MongoDB
Discover the 4 humongous advantages of MongoDB
Change your mindset in 3 key ways
65. #MDBLocal
The mental journey from SQL to MongoDB
Map terms & concepts from SQL to MongoDB
Discover the 4 humongous advantages of MongoDB
Change your mindset in 3 key ways
• Not all documents in a collection need to have the same fields
66. #MDBLocal
The mental journey from SQL to MongoDB
Map terms & concepts from SQL to MongoDB
Discover the 4 humongous advantages of MongoDB
Change your mindset in 3 key ways
• Not all documents in a collection need to have the same fields
• Data that is accessed together should be stored together
67. #MDBLocal
The mental journey from SQL to MongoDB
Map terms & concepts from SQL to MongoDB
Discover the 4 humongous advantages of MongoDB
Change your mindset in 3 key ways
• Not all documents in a collection need to have the same fields
• Data that is accessed together should be stored together
• Stop using transactions regularly