Skip to content

Latest commit

 

History

History
216 lines (136 loc) · 5.17 KB

File metadata and controls

216 lines (136 loc) · 5.17 KB

ERDs, Resource Associations, & MongoDB

Objectives

  1. Master drawing a simple Entity Relationship Diagrams (ERDs) (2-5 resources)
  2. Utilize the common verbiage for defining Resource Associations

Resource Associations

Types of Associations

There are three sorts of resource associations:

  1. One-to-Many or Has Many/Belongs To
  2. One-to-One or Has One/Belongs To
  3. Many-to-Many or Has And Belongs to Many, or Has Many Through

One-to-One

A person has exactly one passport. (A passport belongs to one person.)

one-to-one

One-to-Many

A person has many pets. (A pet belongs to exactly one person.)

one-to-many

Many-to-Many

A person can attend many picnics, and many people can attend one picnic.

many-to-many

Examples

  • [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

Entity Relationship Diagrams — ERDs

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:

  1. One-to-Many: =>
  2. Many-to-Many: <=>
  3. One-to-One: -->

A Blog ERD

Blog erd

A School ERD

School erd

Activity - Drawing ERDs [15 min]

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.

  1. Lyft
  2. Pinterest
  3. Airbnb
  4. Facebook
  5. Apple App Store

Break [10 mins]

Modeling Associations

Modeling Associations Using MongoDB

In a document-based database these Resource Associations are modeled in a few ways. Here they are ordered by frequency

  1. Reference Documents (very common)
  2. Value Associations (pretty common)
  3. Embedded Documents (very rare)

Reference Documents

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")
}

Value Association

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)

Embedded Documents

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)

Activity

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!

Homework

Resources