Video Transcript (English)
We’ve just covered the basics of the document model in this unit, but let’s shift and start to think about data modeling.
It’s important to consider the relationships that exist among your data so that you can leverage MongoDB’s flexible schema to quickly build performant applications.
In this video, we’ll cover some foundational concepts in data modeling for MongoDB.
First, we’ll define two key terms in data modeling, entities and attributes, and how these correspond to documents and collections.
Then we’ll focus on types of relationships among data: one-to-one, one-to-many, and many-to-many. And we’ll use our movies database as an example.
Let’s get started!
Each piece of information in our database can be related to other pieces of information in our database. We call these pieces of information entities.
Entities are unique and independent of each other, and represent distinct objects or concepts within your application.They can be a person, a product, an organization, or a location, for example.
In MongoDB, entities are usually represented by documents and grouped in collections.
For example, in our movies application, we might have entities such as 'Movies, 'Users', and 'Comments'. Each of these entities would correspond to a collection of documents in MongoDB.
Attributes are characteristics that describe an entity. For example, a product's attributes could be the name, description, and price of the product. In MongoDB, attributes are represented as field-value pairs within a document.
For instance, attributes for our movie entity include title, cast, and rating. Each attribute provides specific information about the entity it belongs to.
How we model our data depends on the relationships that exist within it.
There are three primary types of relationships between entities: One-to-One, One-to-Many, and Many-to-Many.
A One-to-One relationship is the most common in MongoDB. One-to-One relationships exist when one entity is related to exactly one other entity.
For example, a movie is released by a single studio.
Therefore, the relationship between the studio and the movie is a one-to-one relationship.
Next, a One-to-Many relationship is when one entity is related to multiple other entities.
For example, a movie typically has multiple cast members. This is an example of a One To Many relationship.
Finally, a Many-to-Many relationship is when multiple entities are related to multiple other entities.
For example, a movie may be shown in many theaters. Moreover, a theater typically shows many different movies at a time.
So,we can conclude that this is a many-to-many relationship.
Nice work! While all this vocabulary may feel a little unfamiliar right now, you’ll be data modeling like a pro in no time.
In this lesson, you learned some foundational principles of data modeling for MongoDB.
Specifically, we defined entities and attributes, and the types of relationships that exist among them in our data:
One-to-one, One-to-many, and Many-to-many.
In the next video, we’ll cover how to model these types of relationships. See you there!