Relationship Modeling

Back

Loading concept...

๐Ÿ˜๏ธ NoSQL Relationship Modeling: A Story of Connections

Imagine youโ€™re building a neighborhood. Houses need to connect to each other, to people, to streets. Some connections are simpleโ€”one person lives in one house. Others are complexโ€”many friends visiting many houses. Letโ€™s explore how data in NoSQL databases connect, just like neighbors in a town!


๐ŸŽฏ What Youโ€™ll Learn

  • One-to-One Relationships (One thing connects to exactly one other thing)
  • One-to-Many Relationships (One thing connects to many things)
  • Many-to-Many Relationships (Many things connect to many things)
  • Cardinality (Counting how many connections exist)

๐Ÿ  The Universal Analogy: The Neighborhood

Think of your database as a neighborhood.

  • Documents are like houses
  • Relationships are like pathways between houses
  • Cardinality tells us how many pathways connect things

Letโ€™s walk through this neighborhood together!


1๏ธโƒฃ One-to-One Relationships

๐Ÿค What Is It?

One thing connects to exactly ONE other thing.

Like having ONE best friend. You have one. They have one. Thatโ€™s it!

๐Ÿ  Neighborhood Example

One House โ†’ One Mailbox

Every house has exactly one mailbox. Every mailbox belongs to exactly one house.

graph TD A["๐Ÿ  House"] --> B["๐Ÿ“ฌ Mailbox"]

๐Ÿ’ป NoSQL Example

Method 1: Embed Inside (Keep Together)

Store the related data INSIDE the main document:

{
  "_id": "user_123",
  "name": "Emma",
  "passport": {
    "number": "AB123456",
    "country": "USA",
    "expires": "2030-01-15"
  }
}

Why? Emma has ONE passport. Keep it together!

Method 2: Reference (Keep Separate)

Store a link to another document:

{
  "_id": "user_123",
  "name": "Emma",
  "passport_id": "passport_789"
}

Separate passport document:

{
  "_id": "passport_789",
  "number": "AB123456",
  "country": "USA",
  "expires": "2030-01-15"
}

โœจ When to Use Each?

Embed (Inside) Reference (Separate)
Data always accessed together Data rarely needed
Small related data Large related data
One user โ†’ One profile One user โ†’ One medical record

๐ŸŽจ Real-World Examples

  • User โ†’ Profile Settings (embed)
  • Person โ†’ Birth Certificate (reference)
  • Car โ†’ License Plate (embed)
  • Employee โ†’ Tax Record (reference)

2๏ธโƒฃ One-to-Many Relationships

๐Ÿ“š What Is It?

ONE thing connects to MANY things.

Like a parent with children. One parent โ†’ many kids!

๐Ÿ  Neighborhood Example

One House โ†’ Many Windows

A house has many windows. Each window belongs to one house.

graph TD A["๐Ÿ  House"] --> B["๐ŸชŸ Window 1"] A --> C["๐ŸชŸ Window 2"] A --> D["๐ŸชŸ Window 3"] A --> E["๐ŸชŸ Window 4"]

๐Ÿ’ป NoSQL Example

Method 1: Embed Array (Many Inside One)

Put all related items inside the parent:

{
  "_id": "blog_post_1",
  "title": "My Day at the Zoo",
  "comments": [
    {
      "user": "Alice",
      "text": "So fun!"
    },
    {
      "user": "Bob",
      "text": "Love the photos!"
    },
    {
      "user": "Carol",
      "text": "Which zoo?"
    }
  ]
}

Method 2: Reference Array (Links to Many)

Store IDs pointing to separate documents:

{
  "_id": "author_1",
  "name": "J.K. Rowling",
  "book_ids": [
    "book_1",
    "book_2",
    "book_3"
  ]
}

Method 3: Parent Reference (Child Points Back)

Each child stores the parentโ€™s ID:

{
  "_id": "order_item_1",
  "product": "Pizza",
  "order_id": "order_100"
}
{
  "_id": "order_item_2",
  "product": "Soda",
  "order_id": "order_100"
}

โœจ Choosing the Right Pattern

Pattern Best When
Embed Array Few items (<100), always accessed together
Reference Array Items accessed separately
Parent Reference Unlimited children, query children often

๐ŸŽจ Real-World Examples

  • Author โ†’ Books (one author writes many books)
  • Customer โ†’ Orders (one customer makes many orders)
  • Category โ†’ Products (one category has many products)
  • Teacher โ†’ Students (one teacher teaches many students)

3๏ธโƒฃ Many-to-Many Relationships

๐Ÿ•ธ๏ธ What Is It?

MANY things connect to MANY other things.

Like students and classes. One student takes MANY classes. One class has MANY students!

๐Ÿ  Neighborhood Example

People โ†” Clubs

Many people join many clubs. Many clubs have many members.

graph TD A["๐Ÿ‘ค Alice"] --> D["๐Ÿ“– Book Club"] A --> E["๐ŸŽฎ Gaming Club"] B["๐Ÿ‘ค Bob"] --> D B --> F["โšฝ Sports Club"] C["๐Ÿ‘ค Carol"] --> E C --> F

๐Ÿ’ป NoSQL Example

Method 1: Two-Way References

Store arrays on BOTH sides:

Student document:

{
  "_id": "student_1",
  "name": "Emma",
  "class_ids": ["math_101", "art_202"]
}

Class document:

{
  "_id": "math_101",
  "name": "Basic Math",
  "student_ids": ["student_1", "student_2"]
}

Method 2: Join Collection (Bridge Table)

Create a THIRD collection to link them:

{
  "_id": "enrollment_1",
  "student_id": "student_1",
  "class_id": "math_101",
  "enrolled_date": "2024-01-15",
  "grade": "A"
}

Why use a join collection?

  • Store extra info about the relationship (grade, date)
  • Handle unlimited connections
  • Avoid array size limits

Method 3: Embed on One Side

When one side has fewer items:

{
  "_id": "book_1",
  "title": "Database Design",
  "authors": [
    {"id": "author_1", "name": "Alice"},
    {"id": "author_2", "name": "Bob"}
  ]
}

โœจ Pattern Comparison

Pattern Pros Cons
Two-Way References Fast queries both ways Must update both docs
Join Collection Extra relationship data Needs extra queries
Embed One Side Simple reads Hard to query embedded side

๐ŸŽจ Real-World Examples

  • Students โ†” Classes (join collection with grades)
  • Products โ†” Tags (two-way references)
  • Users โ†” Roles (embed roles in users)
  • Doctors โ†” Patients (join collection with appointments)

4๏ธโƒฃ Cardinality

๐Ÿ”ข What Is It?

Cardinality = How many connections exist on each side.

Itโ€™s like counting: โ€œHow many?โ€ on each end of the relationship.

๐ŸŽฏ The Four Types

graph TD A["Cardinality Types"] --> B["1:1 One-to-One"] A --> C["1:N One-to-Many"] A --> D["N:1 Many-to-One"] A --> E["N:M Many-to-Many"]

๐Ÿ“Š Visual Guide

Cardinality Symbol Example
1:1 One โ†” One Person โ†” Passport
1:N One โ†” Many Mother โ†” Children
N:1 Many โ†” One Orders โ†” Customer
N:M Many โ†” Many Students โ†” Classes

๐Ÿ’ก Why Cardinality Matters

It helps you CHOOSE the right pattern!

Decision Tree

graph TD A[What's the cardinality?] --> B{1:1?} B -->|Yes| C["Embed or Reference"] B -->|No| D{1:N or N:1?} D -->|Yes| E{How many N?} E -->|Few <100| F["Embed Array"] E -->|Many| G["Reference or Parent Ref"] D -->|No| H["N:M โ†’ Join Collection"]

๐Ÿ” Estimating Cardinality

Ask yourself:

  • Low (1-10): Embed easily
  • Medium (10-100): Embed with caution
  • High (100-1000): Use references
  • Unbounded (1000+): Always use references

Example Analysis

Blog Post โ†’ Comments

โ€œHow many comments per post?โ€

  • New blog: ~10 comments โ†’ Embed โœ…
  • Popular blog: ~1000 comments โ†’ Reference โœ…
// Low cardinality (embed)
{
  "post": "Hello World",
  "comments": ["Great!", "Nice!"]
}

// High cardinality (reference)
{
  "post": "Viral Video",
  "comment_count": 50000
}
// Comments in separate collection

๐ŸŽฏ Quick Summary

Relationship Pattern When to Use
1:1 Embed Data always together
1:1 Reference Large or sensitive data
1:N Embed Array Few children (<100)
1:N Reference Array Moderate children
1:N Parent Ref Unlimited children
N:M Two-Way Refs Query both directions
N:M Join Collection Relationship has extra data

๐Ÿ† You Did It!

You now understand:

โœ… One-to-One: One thing โ†’ One thing (Person โ†’ Passport)

โœ… One-to-Many: One thing โ†’ Many things (Author โ†’ Books)

โœ… Many-to-Many: Many things โ†” Many things (Students โ†” Classes)

โœ… Cardinality: Counting connections to choose the right pattern

Remember our neighborhood:

  • Houses (documents) connect via pathways (relationships)
  • Count the pathways (cardinality) to build the best neighborhood!

Now go build amazing connected data! ๐Ÿš€

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.