Back to Basics
Lauren Schaefer Ken Alger
@Lauren_Schaefer @KenWAlger
Bonus points for
following us on
Twitter
Parks and Recreation, Season 6, Episode 14
Parks and Recreation, Season 6, Episode 14
Back to Basics
Lauren Schaefer Ken Alger
@Lauren_Schaefer @KenWAlger
Slides are
available on our
Twitter pages
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
The story of this workshop is that
it’s about MongoDB
#MongoDB @KenWAlger @Lauren_Schaefer
1. MongoDB terms & concepts
2. Free MongoDB Atlas Cluster
3. CRUD operations
4. Tips & tricks
5. $100 Atlas credits
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
The story of this workshop is that
it’s about MongoDB
#MongoDB @KenWAlger @Lauren_Schaefer
1. MongoDB terms & concepts
2. Free MongoDB Atlas Cluster
3. CRUD operations
4. Tips & tricks
5. $100 Atlas credits
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
MongoDB stores data in documents
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
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
}
]
}
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
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
}
]
}
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
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
}
]
}
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
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
}
]
}
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
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
}
]
}
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
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
}
]
}
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
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
}
]
}
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
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
}
]
}
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
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
}
]
}
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
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
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
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
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
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
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
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
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
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
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
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
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
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
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
Flexible Schema
Don’t panic!
Use schema validation.
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
Flexible Schema
Document Row
{
...
a: “b”
...
}
ID a ...
1 b ...
2 ... ...
3 ... ...
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
Document Row(s)
{
...
a: “b”
...
}
ID a ...
1 b ...
2 ... ...
3 ... ...
... ... ...
... ... ...
... ... ...
... ... ...
... ... ...
... ... ...
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
Field Column
ID a ...
1 b ...
2 c ...
3 ... ...
{
...
a: “b”
...
}
{
...
a: “c”
...
}
#MongoDB @KenWAlger @Lauren_Schaefer#BackToBasics
Collection Table
{
...
}
... ... ...
... ... ...
... ... ...
... ... ...
{
...
}
{
...
}
#MongoDB @KenWAlger @Lauren_Schaefer#BackToBasics
Database Database
... ... ...
... ... ...
... ... ...
... ... ...
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
... ... ...
... ... ...
... ... ...
... ... ...
... ... ...
#MongoDB @KenWAlger @Lauren_Schaefer#BackToBasics
Index Index
{
...
}
{
...
}
{
...
}
{
...
}
... ... ...
... ... ...
... ... ...
... ... ...
... ... ...
#MongoDB @KenWAlger @Lauren_Schaefer#BackToBasics
View View
{
...
}
... ... ...
... ... ...
... ... ...
... ... ...
{
...
}
{
...
}
#MongoDB @KenWAlger @Lauren_Schaefer#BackToBasics
Embedding Join
{
...
a: “b”,
...
c: {
d: “e”
...
},
...
}
ID a ...
1 b ...
2 ... ...
3 ... ...
... d ...
1 e ...
... ... ...
#MongoDB @KenWAlger @Lauren_Schaefer#BackToBasics
Database References Join
ID ... ...
1 ... ...
2 ... ...
3 ... ...
... ... ...
1 ... ...
... ... ...
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
#MongoDB @KenWAlger @Lauren_Schaefer#BackToBasics
$lookup
(Aggregation Pipeline)
Left Outer Join
ID ... ...
1 ... ...
2 ... ...
3 ... ...
... ... ...
1 ... ...
4 ... ...
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
#MongoDB @KenWAlger @Lauren_Schaefer#BackToBasics
$graphLookup
(Aggregation Pipeline)
Recursive Common
Table Expressions
{
...
}
... ... ...
... ... ...
... ... ...
... ... ...
{
...
}
{
...
}
#MongoDB @KenWAlger @Lauren_Schaefer#BackToBasics
Multi-Document ACID
Transaction
Multi-Record ACID
Transaction
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
{
...
}
... ... ...
... ... ...
... ... ...
... ... ...
... ... ...
... ... ...
... ... ...
#MongoDB @KenWAlger @Lauren_Schaefer#BackToBasics
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
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
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
The story of this workshop is that
it’s about MongoDB
1. MongoDB terms & concepts
2. Free MongoDB Atlas Cluster
3. CRUD operations
4. Tips & tricks
5. $100 Atlas credits
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
The story of this workshop is that
it’s about MongoDB
1. MongoDB terms & concepts
2. Free MongoDB Atlas Cluster
3. CRUD operations
4. Tips & tricks
5. $100 Atlas credits
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
The story of this workshop is that
it’s about MongoDB
1. MongoDB terms & concepts
2. Free MongoDB Atlas Cluster
3. CRUD operations
4. Tips & tricks
5. $100 Atlas credits
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
Use Indexes for Read Speed
• Very important for reads.
• However, they come with overhead.
• New in MongoDB 4.2, Wildcard Indexes
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
Indexes support the efficient
execution of queries in MongoDB.
Use Indexes for Read Speed
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
Index Types in MongoDB
Single Field { karma: 1}
Compound Field { karma: 1, user_id: -1 }
Multikey { “address.postal_code”: 1 }
Geospatial
Text
Hashed
Wildcard
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
Model Data Using Schema Design Patterns
• Different way of modeling from the legacy database
paradigm.
• Schema Design is important.
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
Why Do We Create Models?
Ensure:
• Good performance
• Scalability
despite constraints
Hardware
• RAM faster than Disk
• Disk cheaper than RAM
• Network latency
• Reduce costs $$$
Database Server
• Maximum size for a document
Data set
• Size of data
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
• Frequency of Access
• Subset
• Approximation
• Extended Reference
Patterns by Category
• Grouping
• Computed
• Bucket
• Outlier
• Representation
• Attribute
• Schema Versioning
• Document Versioning
• Tree
• Polymorphism
• Pre-Allocation
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
Add a field to track the
schema version number, per
document
Does not have to exist for
version 1
Pattern: Schema Versioning
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
Problem:
Updating the schema of a database is:
• Not atomic
• Long operation
• May not want to update all documents, only do it on updates
Schema Versioning Pattern
Use cases:
Practically any database that will go to production
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
Solution:
Have a field keeping track of the schema version
Schema Versioning Pattern –
Solution
Benefits:
Don't need to update all the documents at once
May not have to update documents until their next modification
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
Reduce Aggravations with the
Aggregation Framework
• Use whenever possible
• Operations are done server-side
• Order of stages matters
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
Aggregation
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
PIPELINE
ps ax | grep mongod| head 1
*nix command line pipe
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
PIPELINE
$match $group | $sort|
Input stream {}
{} {} {}
Result {} {}
...
MongoDB document pipeline
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
The story of this workshop is that
it’s about MongoDB
1. MongoDB terms & concepts
2. Free MongoDB Atlas Cluster
3. CRUD operations
4. Tips & tricks
5. $100 Atlas credits
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
Sign up for MongoDB Atlas
http://bit.ly/MDB_Atlas
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
Add discount code: BACKTOBASICS2020
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
Add discount code: BACKTOBASICS2020
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
Add discount code: BACKTOBASICS2020
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
The story of this workshop is that
it’s about MongoDB
1. MongoDB terms & concepts
2. Free MongoDB Atlas Cluster
3. CRUD operations
4. Tips & tricks
5. $100 Atlas credits
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
1. Map terms 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
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
2. Free Atlas cluster
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
3. The CRUD operations
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
4. Tips & tricks
• Use Indexes for Read Speed
• Model Data Using Schema Design Patterns
• Reduce Aggravation with the Aggregation Pipeline
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
5. $100 Atlas credits with code:
BACKTOBASICS2020
Don’t be Ron Swanson
(in this particular case)
#MongoDB @KenWAlger @Lauren_Schaefer#BackToBasics
Change your mindset &
get the full value of MongoDB
Don’t be Ron Swanson
#MongoDB @KenWAlger @Lauren_Schaefer#BackToBasics
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
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!)
#BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
Additional resources
• The MongoDB Docs
• Quick Start blog series in a variety of programming
languages
• JSON Schema Validation – Locking down your model the
smart way
• JSON Schema Validation - Checking Your Arrays
• M121: The MongoDB Aggregation Framework
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 our Twitter pages:
@KenWAlger
@Lauren_Schaefer
#MongoDB @KenWAlger @Lauren_Schaefer#BackToBasics

MongoDB: Back to Basics

  • 1.
    Back to Basics LaurenSchaefer Ken Alger @Lauren_Schaefer @KenWAlger Bonus points for following us on Twitter
  • 2.
    Parks and Recreation,Season 6, Episode 14
  • 6.
    Parks and Recreation,Season 6, Episode 14
  • 7.
    Back to Basics LaurenSchaefer Ken Alger @Lauren_Schaefer @KenWAlger Slides are available on our Twitter pages
  • 8.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer The story of this workshop is that it’s about MongoDB #MongoDB @KenWAlger @Lauren_Schaefer 1. MongoDB terms & concepts 2. Free MongoDB Atlas Cluster 3. CRUD operations 4. Tips & tricks 5. $100 Atlas credits
  • 9.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer The story of this workshop is that it’s about MongoDB #MongoDB @KenWAlger @Lauren_Schaefer 1. MongoDB terms & concepts 2. Free MongoDB Atlas Cluster 3. CRUD operations 4. Tips & tricks 5. $100 Atlas credits
  • 10.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer MongoDB stores data in documents
  • 11.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer 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 } ] }
  • 12.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer 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 } ] }
  • 13.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer 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 } ] }
  • 14.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer 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 } ] }
  • 15.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer 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 } ] }
  • 16.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer 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 } ] }
  • 17.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer 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 } ] }
  • 18.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer 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 } ] }
  • 19.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer 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 } ] }
  • 20.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer 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
  • 21.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer 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
  • 22.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer 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
  • 23.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer 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
  • 24.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer 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
  • 25.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer 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
  • 26.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer 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
  • 27.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer Flexible Schema
  • 28.
    Don’t panic! Use schemavalidation. #BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer Flexible Schema
  • 29.
    Document Row { ... a: “b” ... } IDa ... 1 b ... 2 ... ... 3 ... ... #BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
  • 30.
    Document Row(s) { ... a: “b” ... } IDa ... 1 b ... 2 ... ... 3 ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... #BackToBasics #MongoDB @KenWAlger @Lauren_Schaefer
  • 31.
    Field Column ID a... 1 b ... 2 c ... 3 ... ... { ... a: “b” ... } { ... a: “c” ... } #MongoDB @KenWAlger @Lauren_Schaefer#BackToBasics
  • 32.
    Collection Table { ... } ... ...... ... ... ... ... ... ... ... ... ... { ... } { ... } #MongoDB @KenWAlger @Lauren_Schaefer#BackToBasics
  • 33.
    Database Database ... ...... ... ... ... ... ... ... ... ... ... { ... } { ... } { ... } { ... } { ... } { ... } { ... } ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... #MongoDB @KenWAlger @Lauren_Schaefer#BackToBasics
  • 34.
    Index Index { ... } { ... } { ... } { ... } ... ...... ... ... ... ... ... ... ... ... ... ... ... ... #MongoDB @KenWAlger @Lauren_Schaefer#BackToBasics
  • 35.
    View View { ... } ... ...... ... ... ... ... ... ... ... ... ... { ... } { ... } #MongoDB @KenWAlger @Lauren_Schaefer#BackToBasics
  • 36.
    Embedding Join { ... a: “b”, ... c:{ d: “e” ... }, ... } ID a ... 1 b ... 2 ... ... 3 ... ... ... d ... 1 e ... ... ... ... #MongoDB @KenWAlger @Lauren_Schaefer#BackToBasics
  • 37.
    Database References Join ID... ... 1 ... ... 2 ... ... 3 ... ... ... ... ... 1 ... ... ... ... ... { ... } { ... } { ... } { ... } { ... } { ... } { ... } #MongoDB @KenWAlger @Lauren_Schaefer#BackToBasics
  • 38.
    $lookup (Aggregation Pipeline) Left OuterJoin ID ... ... 1 ... ... 2 ... ... 3 ... ... ... ... ... 1 ... ... 4 ... ... { ... } { ... } { ... } { ... } { ... } { ... } { ... } #MongoDB @KenWAlger @Lauren_Schaefer#BackToBasics
  • 39.
    $graphLookup (Aggregation Pipeline) Recursive Common TableExpressions { ... } ... ... ... ... ... ... ... ... ... ... ... ... { ... } { ... } #MongoDB @KenWAlger @Lauren_Schaefer#BackToBasics
  • 40.
    Multi-Document ACID Transaction Multi-Record ACID Transaction { ... } { ... } { ... } { ... } { ... } { ... } { ... } ...... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... #MongoDB @KenWAlger @Lauren_Schaefer#BackToBasics
  • 41.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer 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
  • 42.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer The story of this workshop is that it’s about MongoDB 1. MongoDB terms & concepts 2. Free MongoDB Atlas Cluster 3. CRUD operations 4. Tips & tricks 5. $100 Atlas credits
  • 45.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer The story of this workshop is that it’s about MongoDB 1. MongoDB terms & concepts 2. Free MongoDB Atlas Cluster 3. CRUD operations 4. Tips & tricks 5. $100 Atlas credits
  • 48.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer The story of this workshop is that it’s about MongoDB 1. MongoDB terms & concepts 2. Free MongoDB Atlas Cluster 3. CRUD operations 4. Tips & tricks 5. $100 Atlas credits
  • 51.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer Use Indexes for Read Speed • Very important for reads. • However, they come with overhead. • New in MongoDB 4.2, Wildcard Indexes
  • 52.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer Indexes support the efficient execution of queries in MongoDB. Use Indexes for Read Speed
  • 53.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer Index Types in MongoDB Single Field { karma: 1} Compound Field { karma: 1, user_id: -1 } Multikey { “address.postal_code”: 1 } Geospatial Text Hashed Wildcard
  • 54.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer Model Data Using Schema Design Patterns • Different way of modeling from the legacy database paradigm. • Schema Design is important.
  • 55.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer Why Do We Create Models? Ensure: • Good performance • Scalability despite constraints Hardware • RAM faster than Disk • Disk cheaper than RAM • Network latency • Reduce costs $$$ Database Server • Maximum size for a document Data set • Size of data
  • 56.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer • Frequency of Access • Subset • Approximation • Extended Reference Patterns by Category • Grouping • Computed • Bucket • Outlier • Representation • Attribute • Schema Versioning • Document Versioning • Tree • Polymorphism • Pre-Allocation
  • 57.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer Add a field to track the schema version number, per document Does not have to exist for version 1 Pattern: Schema Versioning
  • 58.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer Problem: Updating the schema of a database is: • Not atomic • Long operation • May not want to update all documents, only do it on updates Schema Versioning Pattern Use cases: Practically any database that will go to production
  • 59.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer Solution: Have a field keeping track of the schema version Schema Versioning Pattern – Solution Benefits: Don't need to update all the documents at once May not have to update documents until their next modification
  • 60.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer Reduce Aggravations with the Aggregation Framework • Use whenever possible • Operations are done server-side • Order of stages matters
  • 61.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer Aggregation
  • 62.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer PIPELINE ps ax | grep mongod| head 1 *nix command line pipe
  • 63.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer PIPELINE $match $group | $sort| Input stream {} {} {} {} Result {} {} ... MongoDB document pipeline
  • 64.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer The story of this workshop is that it’s about MongoDB 1. MongoDB terms & concepts 2. Free MongoDB Atlas Cluster 3. CRUD operations 4. Tips & tricks 5. $100 Atlas credits
  • 67.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer Sign up for MongoDB Atlas http://bit.ly/MDB_Atlas
  • 68.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer Add discount code: BACKTOBASICS2020
  • 69.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer Add discount code: BACKTOBASICS2020
  • 70.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer Add discount code: BACKTOBASICS2020
  • 71.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer The story of this workshop is that it’s about MongoDB 1. MongoDB terms & concepts 2. Free MongoDB Atlas Cluster 3. CRUD operations 4. Tips & tricks 5. $100 Atlas credits
  • 72.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer 1. Map terms 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
  • 73.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer 2. Free Atlas cluster
  • 74.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer 3. The CRUD operations
  • 75.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer 4. Tips & tricks • Use Indexes for Read Speed • Model Data Using Schema Design Patterns • Reduce Aggravation with the Aggregation Pipeline
  • 76.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer 5. $100 Atlas credits with code: BACKTOBASICS2020
  • 77.
    Don’t be RonSwanson (in this particular case) #MongoDB @KenWAlger @Lauren_Schaefer#BackToBasics
  • 78.
    Change your mindset& get the full value of MongoDB Don’t be Ron Swanson #MongoDB @KenWAlger @Lauren_Schaefer#BackToBasics
  • 79.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer 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!)
  • 80.
    #BackToBasics #MongoDB @KenWAlger@Lauren_Schaefer Additional resources • The MongoDB Docs • Quick Start blog series in a variety of programming languages • JSON Schema Validation – Locking down your model the smart way • JSON Schema Validation - Checking Your Arrays • M121: The MongoDB Aggregation Framework
  • 81.
    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 our Twitter pages: @KenWAlger @Lauren_Schaefer #MongoDB @KenWAlger @Lauren_Schaefer#BackToBasics