Lesson 3: Modeling Data Relationships / Learn

Press O for more options
2:17

Video Transcript (English)

Hello. In this video, we’ll go through a detailed example of how data might be modeled.

As a quick recap, there are one-to-one, one-to-many, and many-to-many relationships.

And, there are two ways to model relationships: embedding and referencing.

With this in mind, let’s go through an example of data modeling by using the student record shown here.

When a student starts school, they fill out a form in our web application that creates their profile, which then goes in our database.

Looking at this data, we can imagine collecting more pieces of information about this student, such as classes, grades, and so on.

Notice that this student has multiple contact phone numbers: a home phone, a cell phone, and an emergency contact number. This is messy, so we want to reorganize it.

Right now, each contact phone number is entered as a separate item, which illustrates a one-to-one relationship. But, we could also represent this student and his contact phone numbers as a one-to-many relationship by writing the phone numbers in an array, like this.

But when we model the data this way, we lose some context. For example, now we don’t know that one of those numbers is an emergency contact number.

So it would probably be preferable for our model to have more information - In the array, we add the type after the number to specify whether it's the home number, cell phone number, or emergency contact number.

If we have a much larger amount of data on the student that we want to store, such as the classes they’ve taken, we might want to model the data a bit differently. In this case, we can reference another collection or document.

Here we are adding a reference for the courseID, and course name, inside the student’s document, for the courses they’ve taken.

When we look at this other collection for the courses, we see documents that look like this.

Well done! Here are some key takeaways from this video on data modeling:

We can represent relationships in many ways: by inserting data into the document, embedding it, or linking it with a reference.

The example in this video demonstrates a one-to-many relationship with both embedded and linked examples.

Great work, everyone. Now you have an understanding of how flexible MongoDB documents are. You can continue to expand your knowledge by learning more about embedding and referencing in a data model.