- Master drawing a simple Entity Relationship Diagrams (ERDs) (2-5 resources)
- Utilize the common verbiage for defining Resource Associations
There are three sorts of resource associations:
- One-to-Many or Has Many/Belongs To
- One-to-One or Has One/Belongs To
- Many-to-Many or Has And Belongs to Many, or Has Many Through
A person has exactly one passport. (A passport belongs to one person.)
A person has many pets. (A pet belongs to exactly one person.)
A person can attend many picnics, and many people can attend one picnic.
-
[Common] Facebook - One to Many:
- Users have many Posts
- Users have many Comments
- Users have many Likes
-
[Common] Eventbright - Has and Belongs to Many:
- Users have many Events as reservations
- Users belong to many Events as guests
-
[Rare] Eventbright - Has One/Belongs To
- User has one Profile
- User has one Credit Card
Before you code a project, it's a good idea to spend some time thinking about the relationships between objects.
Connect resources in boxes with arrows depending on their relationship:
- One-to-Many:
=>
- Many-to-Many:
<=>
- One-to-One:
-->
Draw ERDs for the core features of 3 the following applications. When you finish each one, check with a partner. Form into groups of 4 and show your favorites off.
- Lyft
- Airbnb
- Apple App Store
In a document-based database these Resource Associations are modeled in a few ways. Here they are ordered by frequency
- Reference Documents (very common)
- Value Associations (pretty common)
- Embedded Documents (very rare)
In this case, each document (e.g. user or post) contains a reference to the _id field of the documents it's related to.
# User has many posts
{
"_id": ObjectId("dsf675as6f6a4s6f")
"name": "Ada Lovelace",
"posts": [ObjectId("a41492308329r900sdf"), ObjectId("9309safd0as0f9f098af")]
}
# Post belongs to user (as author)
{
"_id": ObjectId("a41492308329r900sdf")
"title": "Understanding Model View Controller",
"author": ObjectId("dsf675as6f6a4s6f")
}
In this case, a document contains a reference to the value (usually a name or ID code) of its related documents. The document for a Reddit post might look like this:
{
"title": "Mastering the Three Ball Cascade",
"subreddit": "Jugglers Anonymous"
}
And we can find all posts in a subreddit like this:
# Return all Posts in a specific subreddit:
juggling_posts = db.Posts.find({subreddit: "Jugglers Anonymous"})
for post in juggling_posts:
print(post.title)
Rare for one-to-many associations.
Only use when you always want all children to appear with the parent, or if you don't want to edit the children very much or at all.
embedded_post = {
"title": "Awesome Article",
"comments": [
{ "content": "What a great article" },
{ "content": "Agreed!" }
]
}
db.Posts.insert_one(embedded_post)
Choose one of your resource diagrams and decide whether to model the associations as a Reference to Document, Value Association, or Embedded Document. What are the pros and cons of each?
If you finish early, work on your Playlister tutorial!
-
Playlister tutorial due on Monday, Dec 9
-
Contractor Project due on Thursday, Dec 12