In this video I want to take a moment to introduce a really really important concept called associations
so associations will allow us to have multiple pieces of data multiple collections in our database that
are related to one another.
And this is hugely important to making more complex web apps.
Right now we have Yelp camp and your camp has campgrounds in it.
But then we're going to add in users.
Want to be able to have users who then are associated with campgrounds and then campgrounds will have
comments and we want to have those campgrounds associated with a list of comments or if we take an example
like Facebook on Facebook you sign up.
So there's an entity a model called user and then there's another entity called post and then somewhere
there's photos but there's also albums and there's comments and there's tags and likes.
There are way more models that I'm not mentioning but all of these are interconnected.
So a user is related to a post when I create a post.
That post is not just an arbitrary post.
It's related to me.
Same thing with the photos that I upload and the albums that those photos belong to.
So a photo can be associated with an album and an album can be associated with a user.
And we have comments.
A comment can be on a photo the comment can be on a post.
I think a comment can also be on an album of photos than we have tags so photos can have tags but you
can also tag people and comments.
You can tag people in posts we have likes.
You can like comment.
You can like an album you can like a photo you can like a post.
There is so many different things going on but the core concept is that data is related.
The term associations just refers to this idea of having associated data and the other important point
is that there are different types of associations there are associations that are known as one to one
one to many and many to many relationships.
So start by talking about one to one relationships and one to one are the simplest relationships.
So we have one of an entity that's related to one another entity so we could say that one book has one
publisher or an employee has one title where it gets more exciting as we start talking about one to
many relationships and one to many relationship one entity.
It's related to many of another entity.
We can take the example of Facebook one user can have multiple photos that they have uploaded the photos
belong to that user but those photos belong to one user.
A photo can not have multiple uploads so it's a one to many relationship unlike a many to many relationship
where the association goes both ways.
The classic example of a many to many relationship is students and courses.
So in college a student can sign up for multiple courses and each course has multiple students enrolled
.
So its this two way many to many relationship.
Another one would be books and authors.
So an author can write can have many books and each book can also have many authors.
It might be more common for there to only be one author but it's definitely possible and not uncommon
to have multiple authors.
So those are the three basic types of relationships and the type that will encounter the most is one
too many.
So we'll have comments and campgrounds and one campground has many comments but one comment only belongs
to one campground in the next two videos.
I'm going to introduce how we can write associations how we can connect different models using mongoose
and I'm going to show two different ways of doing that.
The first is using what's known as embedding data and the second one it's known as referencing data
.
So I'm going to do these in two separate installments.
And coming up next I'm going to show you how we can embed data to Associated