1
00:00:08,119 --> 00:00:09,380
Welcome back.

2
00:00:09,410 --> 00:00:16,219
In this video, let's finish up the embedded way of database relationship.

3
00:00:16,610 --> 00:00:17,110
Good.

4
00:00:17,120 --> 00:00:18,620
So here we go.

5
00:00:18,650 --> 00:00:25,310
The first step is that we need to find the author who is creating the posts.

6
00:00:25,460 --> 00:00:31,520
And lucky for us, we are passing in the author as the ID here.

7
00:00:32,360 --> 00:00:34,250
So let's get back to the post.

8
00:00:34,370 --> 00:00:37,930
So the first step here is let's have some roadmap.

9
00:00:37,940 --> 00:00:39,740
The first step is what?

10
00:00:39,740 --> 00:00:49,130
Find the author because we need the author ID before we can create and we can get that one from where

11
00:00:49,130 --> 00:00:50,300
they request.

12
00:00:50,300 --> 00:00:54,200
Dot body dot author which is this.

13
00:00:54,410 --> 00:00:56,150
So now here we go.

14
00:00:56,150 --> 00:01:08,720
Cost author is equal to let's await the requests and then user I have the user model dot find by ID

15
00:01:08,720 --> 00:01:16,850
and I'll get it from rec dot body dot alpha as that.

16
00:01:16,850 --> 00:01:20,980
Now here we assume that the user is being found.

17
00:01:20,990 --> 00:01:23,420
Well, we can check if there is no user.

18
00:01:23,420 --> 00:01:26,740
But for this case let's ignore the error handling.

19
00:01:26,750 --> 00:01:29,630
So at this point we assume there is a user.

20
00:01:29,660 --> 00:01:34,700
Step number two is we need to create a new.

21
00:01:37,420 --> 00:01:41,020
Post and that is what we are doing here.

22
00:01:41,320 --> 00:01:46,360
So remember creating a post you need to pass in the author.

23
00:01:46,390 --> 00:01:48,920
That's how we have specified here.

24
00:01:48,940 --> 00:01:54,270
So inside the post, let's provide the author and the author.

25
00:01:54,280 --> 00:01:57,190
We can get it from when rec.

26
00:01:59,020 --> 00:02:00,520
Dart body.

27
00:02:01,560 --> 00:02:04,740
Dot author and we are done with that.

28
00:02:05,310 --> 00:02:14,490
The next step is remember when creating, we want to push all the posts the user has created into the

29
00:02:14,490 --> 00:02:16,300
fields of this user.

30
00:02:16,320 --> 00:02:18,150
So here we go.

31
00:02:18,390 --> 00:02:22,920
After creating the step number three is that push.

32
00:02:23,770 --> 00:02:25,330
The Post.

33
00:02:26,540 --> 00:02:31,700
Into the users post field as that.

34
00:02:31,700 --> 00:02:33,230
So how are you going to do it?

35
00:02:33,230 --> 00:02:40,030
Remember we have the author here and on that we have a field called Post which is this.

36
00:02:40,040 --> 00:02:43,340
So here we go when I be the author, sorry, author.

37
00:02:43,340 --> 00:02:51,110
Instead we have a property called Post and then we are going to use a real method called push and we're

38
00:02:51,110 --> 00:02:59,360
going to push the entire post into that because here we want to save all the post into that and this

39
00:02:59,360 --> 00:03:01,520
is what you call the embedded meaning.

40
00:03:01,520 --> 00:03:06,140
We are embedding the whole contents of a document into one field.

41
00:03:06,140 --> 00:03:11,570
In this case it is a post document, so let's pass in the saved post.

42
00:03:12,050 --> 00:03:18,800
So because we have make changes to the user's document, we need to save the author.

43
00:03:18,800 --> 00:03:22,160
So step number four, I'm going to be re save.

44
00:03:24,310 --> 00:03:25,840
The author.

45
00:03:26,350 --> 00:03:27,580
And here we go.

46
00:03:27,610 --> 00:03:33,280
We are going to await the process and going to be the author.

47
00:03:33,620 --> 00:03:35,830
Dot save.

48
00:03:36,900 --> 00:03:38,670
I is that good?

49
00:03:38,850 --> 00:03:40,710
And that is it.

50
00:03:40,740 --> 00:03:49,920
So lastly, we need to send the response step number five and maybe send the response to clients.

51
00:03:49,920 --> 00:03:52,050
And that is what we are doing it here.

52
00:03:52,080 --> 00:03:54,240
So now moment of truth.

53
00:03:54,390 --> 00:03:56,010
Back to post here.

54
00:03:56,040 --> 00:03:59,130
Now let's check the response after clicking.

55
00:03:59,160 --> 00:04:03,120
Now have a look when I hit send, let's wait.

56
00:04:03,120 --> 00:04:06,690
And this time around something is different.

57
00:04:06,960 --> 00:04:13,650
On the post we have a field called author but instead it is an ID.

58
00:04:13,680 --> 00:04:22,980
So this simply means that we are saving the author or the user who is creating this post onto the post

59
00:04:22,980 --> 00:04:23,430
field.

60
00:04:24,030 --> 00:04:33,210
And if I fetch all users, I'll be able to see the post that this user has been created.

61
00:04:33,210 --> 00:04:36,240
So let's click on all users to fetch all users.

62
00:04:36,240 --> 00:04:39,000
So here is a request for users.

63
00:04:39,330 --> 00:04:42,810
Now let's send let's have a look.

64
00:04:42,810 --> 00:04:53,880
This time around we have the full name country and inside the post we have one post being created by

65
00:04:53,880 --> 00:04:54,810
this user.

66
00:04:55,260 --> 00:05:02,760
All right, I can go ahead and create a new post with the same author, I can say.

67
00:05:03,640 --> 00:05:06,010
Express, Jess.

68
00:05:06,010 --> 00:05:08,680
And I would say it is excellent.

69
00:05:08,830 --> 00:05:16,360
Let's make the requests now and let's check it out here and let's fetch our users and let's see, we

70
00:05:16,360 --> 00:05:17,560
have two posts.

71
00:05:17,590 --> 00:05:26,040
Post number one is not jazz and post number two is called the Excellent Express.

72
00:05:26,050 --> 00:05:27,310
That is it.

73
00:05:27,400 --> 00:05:34,180
And if I fetch all posts, I'll be able to see the user.

74
00:05:34,210 --> 00:05:35,430
So here we go.

75
00:05:35,440 --> 00:05:44,350
When I made the request, we see that I have to posts with all five property and this one ask that.

76
00:05:44,350 --> 00:05:52,990
So I think you would be thinking that you want to get the real data about this author by not the ID.

77
00:05:53,140 --> 00:06:02,560
So on the author, like I told you that we can save the entire user into this by making use of this

78
00:06:02,560 --> 00:06:04,570
one as an object.

79
00:06:04,570 --> 00:06:09,460
So inside the root we can use the user found, which is this one.

80
00:06:09,460 --> 00:06:10,510
Let me show you here.

81
00:06:10,540 --> 00:06:18,910
This is a found and then we save it inside the author here or let me show you the recommended way.

82
00:06:18,910 --> 00:06:21,340
So back to the post model.

83
00:06:21,460 --> 00:06:31,300
Instead of just doing this way, we are going to use a special type called Mongoose dot schema.

84
00:06:31,330 --> 00:06:40,780
As I said before, now this is a time schema, dot types, dot object ID.

85
00:06:40,810 --> 00:06:47,950
They are all common cases with the exception of Mongoose, and for this one it means that we are going

86
00:06:47,950 --> 00:06:51,040
to save the IDs of the user.

87
00:06:51,040 --> 00:06:58,300
Only the next property is going to be as RDF, which means reference, meaning that which one are we

88
00:06:58,300 --> 00:06:59,410
referencing?

89
00:06:59,530 --> 00:07:07,780
In this case we are referencing the user model because we are saying that this author represents the

90
00:07:07,780 --> 00:07:12,310
user, meaning the user model or the instance of the user.

91
00:07:12,310 --> 00:07:18,520
So make sure that you type in the exact name as your model name for users.

92
00:07:18,520 --> 00:07:28,270
In this way we have a special method to get or to convert the IDs into real object.

93
00:07:28,270 --> 00:07:36,910
So inside the rand for finding out how we change the query like that, we can also change what is called

94
00:07:36,910 --> 00:07:37,930
populate.

95
00:07:38,260 --> 00:07:48,550
As the name implies, the populate is used to convert all the IDs into the normal object and here we

96
00:07:48,550 --> 00:07:50,620
provide the property name.

97
00:07:50,830 --> 00:07:58,750
If you go to the post model here we see that we have the author field and this is what I want to come

98
00:07:58,750 --> 00:08:01,690
back to normal user or populate.

99
00:08:01,690 --> 00:08:07,480
So now inside the root our mix of what's the author.

100
00:08:07,690 --> 00:08:14,800
If I go to fetch all posts here this time around now let's check when I make the requests.

101
00:08:14,800 --> 00:08:21,820
Well, we see that now the IDs has been converted into the normal user.

102
00:08:21,820 --> 00:08:22,750
How is that?

103
00:08:22,750 --> 00:08:24,520
How awesome it is.

104
00:08:24,520 --> 00:08:28,150
So this is what we call the embedded way.

105
00:08:28,300 --> 00:08:35,169
But for some reason too we are using reference, but in this case it's not the normal reference.

106
00:08:35,470 --> 00:08:38,710
But I've given you a little bit of referencing.

107
00:08:38,740 --> 00:08:42,700
In the next video I'm going to show you the most recommended way.

108
00:08:42,700 --> 00:08:48,420
That is what you call the referencing by using one to many relationship.

109
00:08:48,430 --> 00:08:55,120
So for the same concept you are going to use different methods to achieve the same idea or concept.

