1
00:00:00,160 --> 00:00:05,290
In this video I want to take a moment to introduce a really really important concept called associations

2
00:00:05,790 --> 00:00:11,130
so associations will allow us to have multiple pieces of data multiple collections in our database that

3
00:00:11,130 --> 00:00:12,570
are related to one another.

4
00:00:12,840 --> 00:00:16,200
And this is hugely important to making more complex web apps.

5
00:00:16,200 --> 00:00:20,800
Right now we have Yelp camp and your camp has campgrounds in it.

6
00:00:21,090 --> 00:00:22,860
But then we're going to add in users.

7
00:00:22,890 --> 00:00:27,840
Want to be able to have users who then are associated with campgrounds and then campgrounds will have

8
00:00:27,840 --> 00:00:33,210
comments and we want to have those campgrounds associated with a list of comments or if we take an example

9
00:00:33,210 --> 00:00:35,750
like Facebook on Facebook you sign up.

10
00:00:35,790 --> 00:00:43,140
So there's an entity a model called user and then there's another entity called post and then somewhere

11
00:00:43,140 --> 00:00:50,690
there's photos but there's also albums and there's comments and there's tags and likes.

12
00:00:50,730 --> 00:00:55,050
There are way more models that I'm not mentioning but all of these are interconnected.

13
00:00:55,290 --> 00:00:58,950
So a user is related to a post when I create a post.

14
00:00:58,980 --> 00:01:01,260
That post is not just an arbitrary post.

15
00:01:01,260 --> 00:01:03,100
It's related to me.

16
00:01:03,120 --> 00:01:08,050
Same thing with the photos that I upload and the albums that those photos belong to.

17
00:01:08,070 --> 00:01:13,860
So a photo can be associated with an album and an album can be associated with a user.

18
00:01:14,010 --> 00:01:15,330
And we have comments.

19
00:01:15,330 --> 00:01:18,540
A comment can be on a photo the comment can be on a post.

20
00:01:18,640 --> 00:01:24,450
I think a comment can also be on an album of photos than we have tags so photos can have tags but you

21
00:01:24,450 --> 00:01:26,120
can also tag people and comments.

22
00:01:26,160 --> 00:01:28,820
You can tag people in posts we have likes.

23
00:01:28,950 --> 00:01:29,960
You can like comment.

24
00:01:29,970 --> 00:01:32,680
You can like an album you can like a photo you can like a post.

25
00:01:32,680 --> 00:01:37,800
There is so many different things going on but the core concept is that data is related.

26
00:01:37,920 --> 00:01:43,680
The term associations just refers to this idea of having associated data and the other important point

27
00:01:43,830 --> 00:01:48,750
is that there are different types of associations there are associations that are known as one to one

28
00:01:49,440 --> 00:01:53,360
one to many and many to many relationships.

29
00:01:53,490 --> 00:01:58,740
So start by talking about one to one relationships and one to one are the simplest relationships.

30
00:01:58,830 --> 00:02:06,000
So we have one of an entity that's related to one another entity so we could say that one book has one

31
00:02:06,000 --> 00:02:11,640
publisher or an employee has one title where it gets more exciting as we start talking about one to

32
00:02:11,640 --> 00:02:16,000
many relationships and one to many relationship one entity.

33
00:02:16,020 --> 00:02:18,270
It's related to many of another entity.

34
00:02:18,420 --> 00:02:24,540
We can take the example of Facebook one user can have multiple photos that they have uploaded the photos

35
00:02:24,540 --> 00:02:28,710
belong to that user but those photos belong to one user.

36
00:02:28,710 --> 00:02:35,550
A photo can not have multiple uploads so it's a one to many relationship unlike a many to many relationship

37
00:02:35,850 --> 00:02:38,250
where the association goes both ways.

38
00:02:38,260 --> 00:02:43,440
The classic example of a many to many relationship is students and courses.

39
00:02:43,440 --> 00:02:49,950
So in college a student can sign up for multiple courses and each course has multiple students enrolled

40
00:02:49,950 --> 00:02:50,280
.

41
00:02:50,340 --> 00:02:53,310
So its this two way many to many relationship.

42
00:02:53,400 --> 00:02:55,680
Another one would be books and authors.

43
00:02:55,980 --> 00:03:03,150
So an author can write can have many books and each book can also have many authors.

44
00:03:03,150 --> 00:03:08,400
It might be more common for there to only be one author but it's definitely possible and not uncommon

45
00:03:08,400 --> 00:03:09,710
to have multiple authors.

46
00:03:09,870 --> 00:03:14,850
So those are the three basic types of relationships and the type that will encounter the most is one

47
00:03:14,850 --> 00:03:15,710
too many.

48
00:03:15,780 --> 00:03:23,250
So we'll have comments and campgrounds and one campground has many comments but one comment only belongs

49
00:03:23,250 --> 00:03:26,640
to one campground in the next two videos.

50
00:03:26,670 --> 00:03:31,470
I'm going to introduce how we can write associations how we can connect different models using mongoose

51
00:03:31,760 --> 00:03:34,040
and I'm going to show two different ways of doing that.

52
00:03:34,050 --> 00:03:40,680
The first is using what's known as embedding data and the second one it's known as referencing data

53
00:03:40,690 --> 00:03:41,270
.

54
00:03:41,970 --> 00:03:44,520
So I'm going to do these in two separate installments.

55
00:03:44,520 --> 00:03:48,120
And coming up next I'm going to show you how we can embed data to Associated
