1
00:00:08,750 --> 00:00:10,010
In this video.

2
00:00:10,010 --> 00:00:16,430
Let's talk about how we can relate to or more document together.

3
00:00:16,790 --> 00:00:26,420
And the first hype is what we call embedding method, meaning we can relate to or more document together

4
00:00:26,420 --> 00:00:30,560
using this method called embedding method.

5
00:00:30,560 --> 00:00:33,860
And I will explain to you as we move on.

6
00:00:34,010 --> 00:00:37,730
The next hype is what you call referencing.

7
00:00:37,730 --> 00:00:41,540
So among these, which one should we use?

8
00:00:41,540 --> 00:00:46,310
Well, let's dive in more to talk about each of these type.

9
00:00:46,340 --> 00:00:50,090
Let's start with embedding method.

10
00:00:50,240 --> 00:00:55,910
So what is embedding way of relating document together?

11
00:00:56,330 --> 00:00:58,100
Let me show you this picture.

12
00:00:58,100 --> 00:01:06,020
Well, to understand what is called embedding in this case, let's say we are developing a blog application

13
00:01:06,020 --> 00:01:08,180
and we have two collections.

14
00:01:08,180 --> 00:01:14,930
One collection is called the user collection, and this is what you call the user schema.

15
00:01:15,200 --> 00:01:22,520
And you can see that for this user schema, we have these properties called Full Name Country and then

16
00:01:22,520 --> 00:01:30,020
post where I will come up to this and let's say that you also have two posts post one and then post

17
00:01:30,020 --> 00:01:39,260
two and each post it has a title and then ID call post one ID and then post to ID.

18
00:01:39,260 --> 00:01:47,720
So these two posts are from post collection and then this one is from where user collection.

19
00:01:47,720 --> 00:01:51,110
So it means that we have two collections.

20
00:01:51,110 --> 00:01:53,590
So how are we going to relate?

21
00:01:53,600 --> 00:02:02,510
Simply means that if I want to know how many posts are all post that has been created by a certain user,

22
00:02:02,540 --> 00:02:10,009
we need what is called relating document together and the first type is what you call the embedding.

23
00:02:10,009 --> 00:02:11,750
So what does it mean?

24
00:02:12,080 --> 00:02:16,970
Let's have a look at the user's collection or the user schema here.

25
00:02:17,240 --> 00:02:27,380
If you look at this field, we have post and in there we have arrays of post ID and the post ID one

26
00:02:27,380 --> 00:02:34,280
represent this post and then post number to represent this post to.

27
00:02:34,280 --> 00:02:39,080
In this case we are relating to document together.

28
00:02:39,290 --> 00:02:48,950
So it means that if I fetch user, I'm going to see all the posts that I have created and this is what

29
00:02:48,950 --> 00:02:58,340
we call the embedding, meaning that we are embedding the IDs into the fields of post of the user,

30
00:02:58,760 --> 00:03:08,030
and for embedding we can take the whole object and then place it inside the post array than the ID.

31
00:03:08,180 --> 00:03:16,640
We can use the entire object and place it inside as that, or we can use the IDs and if we want to fetch

32
00:03:16,640 --> 00:03:23,450
we are going to look through the collection and then convert all these IDs into normal posts.

33
00:03:23,450 --> 00:03:27,140
And this is what we call the embedding way.

34
00:03:27,290 --> 00:03:33,500
But guys, this option has some limitations and drawbacks.

35
00:03:33,770 --> 00:03:43,460
Let's take it that if I want to find or in my profile page, I don't want to fetch my post, but because

36
00:03:43,460 --> 00:03:51,350
we have embedded, it means that any time we make requests to the user or the post this user has made

37
00:03:51,380 --> 00:03:52,310
will be fetched.

38
00:03:52,670 --> 00:03:56,540
Let's take it that this user has created 1000 post.

39
00:03:56,570 --> 00:04:00,050
You can see how we are going to consume resources.

40
00:04:00,320 --> 00:04:09,380
And again, MongoDB has limitation on how many size of documents a particular field can take or a collection

41
00:04:09,380 --> 00:04:10,250
can take.

42
00:04:10,250 --> 00:04:19,459
Again, another limitation is that let's say that in future I have updated post number one to post number

43
00:04:19,459 --> 00:04:20,690
for a title.

44
00:04:21,260 --> 00:04:28,250
So I need to come to my collection of users and also update that post manually.

45
00:04:28,250 --> 00:04:34,640
Or I can use what is called middleware for Mongoose to do that work and wouldn't it be a little bit

46
00:04:34,640 --> 00:04:35,480
difficult?

47
00:04:35,750 --> 00:04:41,300
But in some cases you may need this kind of embedding in some use cases.

48
00:04:41,300 --> 00:04:49,700
Case number one is that if you know that this field isn't going to consume or contain a lot of documents,

49
00:04:49,700 --> 00:04:52,190
then this is the best way to go.

50
00:04:52,980 --> 00:05:00,150
And again, if you know that the document inside this array is not going to be updated frequently in

51
00:05:00,150 --> 00:05:03,300
the future, then this is the best way to go.

52
00:05:03,630 --> 00:05:08,730
So what is the next type of relating the command together?

53
00:05:08,970 --> 00:05:12,740
And that is what we call the reference end.

54
00:05:12,780 --> 00:05:16,020
This is the most recommended.

55
00:05:16,230 --> 00:05:16,740
All right.

56
00:05:16,740 --> 00:05:24,210
So what is referencing again, let's say we have the same collection from the previous video.

57
00:05:24,240 --> 00:05:30,030
We have user module or user schema, and then we have also the post schema.

58
00:05:30,420 --> 00:05:35,520
But before I explain, let's look at the files inside each schema.

59
00:05:35,910 --> 00:05:43,350
For the user, we have the full name called John Doe and then country called UAC.

60
00:05:44,110 --> 00:05:52,960
And then on the post we have a title and then the user who is creating this particular post.

61
00:05:53,350 --> 00:06:01,750
Well, it will say that inside we are using special syntax called mongoose dot schema, dot types,

62
00:06:01,750 --> 00:06:08,320
dot object ID and we are referencing the user, which is this.

63
00:06:08,740 --> 00:06:16,690
So when you are using referencing these two documents are independent, they are not related together.

64
00:06:16,840 --> 00:06:25,420
But because we use what is called mongoose schema dot hypes, dot object ID and the user meaning the

65
00:06:25,420 --> 00:06:29,500
reference we are using what is called the reference in way.

66
00:06:29,680 --> 00:06:35,020
Now here comes the question that I know you may be thinking of.

67
00:06:35,440 --> 00:06:43,150
Question number one is that how will I know which user created a particular post?

68
00:06:43,150 --> 00:06:45,220
And it's pretty simple one.

69
00:06:45,220 --> 00:06:51,640
You could see that we have the user here, meaning that when you are creating a post you need to provide

70
00:06:51,640 --> 00:06:59,980
the ID and this one means the mongoose ID, meaning the user ID and you provide it here.

71
00:06:59,980 --> 00:07:05,890
So right now we will know which user has created this particular post.

72
00:07:05,890 --> 00:07:09,730
Right now, next question is how will I know?

73
00:07:09,730 --> 00:07:12,820
How many posts have I created?

74
00:07:12,850 --> 00:07:17,050
Because there is no field or post on the user model.

75
00:07:17,500 --> 00:07:22,930
But behind the scene we call something virtual properties.

76
00:07:22,930 --> 00:07:28,750
When we say virtual properties, they are properties that are not on this schema.

77
00:07:29,570 --> 00:07:39,110
But when we execute a method, for example, find we can populate those virtual fields upon making a

78
00:07:39,110 --> 00:07:47,540
request like find or find or find by ID and you will see those fields in the next video.

79
00:07:47,570 --> 00:07:56,840
Let's start with how we can model a data where a user can create many posts and a post can belong to

80
00:07:56,840 --> 00:07:57,830
a single user.

