1
00:00:08,080 --> 00:00:09,310
In this video.

2
00:00:09,310 --> 00:00:18,220
Let's start with how we can model data after we have explained the concept in the previous video.

3
00:00:18,610 --> 00:00:27,400
Do you remember the first step is what the planning stage to be able to get the idea behind the project.

4
00:00:27,520 --> 00:00:35,530
So in what case, let's say that we are going to develop a blog application so we need users and then

5
00:00:35,530 --> 00:00:36,250
post.

6
00:00:36,280 --> 00:00:40,360
Now we are done with the ideation in the planning stage.

7
00:00:40,360 --> 00:00:41,710
We have the idea.

8
00:00:41,890 --> 00:00:45,400
The next step is we need to have a database name.

9
00:00:45,550 --> 00:00:50,320
So let's go ahead inside our utils under DB.

10
00:00:50,350 --> 00:00:54,130
Let's provide a database name for this project.

11
00:00:54,130 --> 00:01:02,590
And after this slash, let's provide the database name and this is what you use in the previous video.

12
00:01:02,590 --> 00:01:08,470
But because this one is a block application, I will name this one as block app.

13
00:01:08,830 --> 00:01:14,950
Well, you need to use your own connection string, but I will provide this one for you.

14
00:01:14,950 --> 00:01:19,210
But in case I'm not happy, I can delete the password.

15
00:01:19,390 --> 00:01:20,020
Okay.

16
00:01:20,320 --> 00:01:20,710
All right.

17
00:01:20,710 --> 00:01:24,040
So just by the way, so let's go ahead and save it.

18
00:01:24,040 --> 00:01:28,000
And now the next step is let's run our server.

19
00:01:28,000 --> 00:01:34,900
So let's copy the terminal here and let's go ahead and make use of MP and run server.

20
00:01:34,930 --> 00:01:38,080
Now our server is running.

21
00:01:38,230 --> 00:01:42,520
The next step is we need to have a data to work with.

22
00:01:42,520 --> 00:01:48,910
Now we are done with the database and we have our collections which are our models.

23
00:01:48,910 --> 00:01:57,070
The next step is we need to create dummy data and we can use Postman to do that, or we can use the

24
00:01:57,070 --> 00:02:02,800
built in extension that we have discussed before CORE ten to client.

25
00:02:02,800 --> 00:02:10,090
So if you don't have it, go to extension and search for tender client and that is it.

26
00:02:10,330 --> 00:02:16,480
And after that, open it and click on collection and you can see that for me.

27
00:02:16,480 --> 00:02:24,910
I have one collection called data modeling and inside I have two folders which represents my collection.

28
00:02:24,910 --> 00:02:27,720
So I have post and then uses.

29
00:02:27,760 --> 00:02:33,460
If we want to know how to create folders, click on this three dots and you are good to go.

30
00:02:33,490 --> 00:02:39,880
So let's go ahead and register a user first before we can start with our database modeling.

31
00:02:39,880 --> 00:02:43,810
So after clicking, let's go ahead and make the request.

32
00:02:43,810 --> 00:02:48,640
I'm registering a user with full name Joe and country called Ghana.

33
00:02:48,670 --> 00:02:51,850
Let me hit send now let's check.

34
00:02:51,880 --> 00:02:56,200
It has been created if I check my compass.

35
00:02:56,230 --> 00:03:03,190
Now I have also connected my connection string with this and if I refresh it, it will see that I'm

36
00:03:03,190 --> 00:03:07,270
going to have different database called BLOCK app.

37
00:03:07,420 --> 00:03:08,500
And there you go.

38
00:03:08,500 --> 00:03:10,720
You can see that we have BLOCK App.

39
00:03:10,810 --> 00:03:13,390
This is what we have worked on before.

40
00:03:13,420 --> 00:03:20,800
So a click on the block app and you can see that we have two collection co posts and uses.

41
00:03:20,800 --> 00:03:26,110
We can achieve the same thing using the extension for MongoDB.

42
00:03:26,140 --> 00:03:29,560
So I'll click on my connection string for this one too.

43
00:03:29,590 --> 00:03:35,410
We have talked about it before and it has been connected, so let's have a look.

44
00:03:35,440 --> 00:03:40,330
Our database and collection right inside Visual Studio code.

45
00:03:40,510 --> 00:03:40,890
Good.

46
00:03:40,900 --> 00:03:44,350
So you see that I have one database called BLOCK App.

47
00:03:44,350 --> 00:03:47,950
If I open it, I have two collections posts and users.

48
00:03:47,950 --> 00:03:51,160
If I click on post, there is no the command zero.

49
00:03:51,160 --> 00:03:53,530
But for users I have one.

50
00:03:53,530 --> 00:04:00,550
If I open it, you're going to see that this is a user that I have saved into my database.

51
00:04:00,550 --> 00:04:02,260
How awesome it is.

52
00:04:02,380 --> 00:04:05,740
The next step is let's go ahead and create a post.

53
00:04:05,740 --> 00:04:13,240
So on the posts click on Create and now let's go ahead and create the posts.

54
00:04:13,570 --> 00:04:21,339
For the meantime, we move the author here and then pass in the title of the post and then the content.

55
00:04:21,339 --> 00:04:24,730
So let me hit send and there we go.

56
00:04:25,480 --> 00:04:32,050
Well, up to this point, there is no relationship or there is no referencing or there is no embedding

57
00:04:32,050 --> 00:04:32,800
over here.

58
00:04:32,800 --> 00:04:38,800
So now we have these two records to work with a post and then a user.

59
00:04:38,800 --> 00:04:42,160
So let me collapse this one and this one also.

60
00:04:42,280 --> 00:04:46,930
Let's get back to our database and let's refresh it now.

61
00:04:46,930 --> 00:04:48,850
I want to show you something here.

62
00:04:49,120 --> 00:04:58,420
If I click on posts, you can see that we have one post and this post we don't know which user created

63
00:04:58,420 --> 00:04:59,350
this post.

64
00:04:59,620 --> 00:05:01,390
Likewise for users.

65
00:05:01,390 --> 00:05:07,540
If I click on users, I don't know what post does this user has created.

66
00:05:07,710 --> 00:05:11,940
Because we have not done what is called relationship.

67
00:05:11,970 --> 00:05:15,240
So to get it done, let's get back to code.

68
00:05:15,360 --> 00:05:19,230
Now we are going to work inside the model.

69
00:05:19,260 --> 00:05:21,330
So here we go.

70
00:05:22,050 --> 00:05:26,220
First is let's open the post model.

71
00:05:26,490 --> 00:05:33,960
The first thing that comes into your mind is that if you are creating a post differently, you need

72
00:05:33,960 --> 00:05:36,330
to provide your ID.

73
00:05:36,480 --> 00:05:45,660
So on the post we need to have a reference or identity which is going to represent a user who is creating

74
00:05:45,660 --> 00:05:46,600
this post.

75
00:05:46,620 --> 00:05:51,930
So I'm going to make this of author or we can make use of user.

76
00:05:51,960 --> 00:05:53,760
The choice is yours.

77
00:05:53,790 --> 00:05:57,340
So this is a process of what is called embedding.

78
00:05:57,360 --> 00:06:02,120
First, we are representing a user inside the schema.

79
00:06:02,130 --> 00:06:03,300
So here we go.

80
00:06:03,450 --> 00:06:05,480
We provide a type.

81
00:06:05,490 --> 00:06:07,590
We're going to be a string.

82
00:06:08,370 --> 00:06:09,060
That is it.

83
00:06:09,060 --> 00:06:12,920
And then it's going to be a required field.

84
00:06:12,930 --> 00:06:15,540
So we're going to be as true.

85
00:06:15,570 --> 00:06:25,050
Now we have an identity on the post schema, meaning that when creating a post you need to provide your

86
00:06:25,050 --> 00:06:25,890
ID.

87
00:06:25,920 --> 00:06:35,520
So for this one, we can make use of a special method or a special syntax for mongoose called mongoose

88
00:06:35,520 --> 00:06:38,340
dot schema dot object id.

89
00:06:38,370 --> 00:06:44,610
Meaning that we want to save the ID only again for this one too.

90
00:06:44,640 --> 00:06:48,840
We can save the entire user object.

91
00:06:48,840 --> 00:06:57,120
So for this method we can make use of the entire user object, including the foo name, the city and

92
00:06:57,120 --> 00:06:58,950
any property about the user.

93
00:06:59,100 --> 00:07:05,580
But if we want to use a specific field, let's say a full name of the user and provide a string.

94
00:07:06,240 --> 00:07:14,150
But always it is advisable to use the ID of the user and you can go by using string.

95
00:07:14,430 --> 00:07:26,250
But Mongoose have a special type called Mongoose Dart Schema Uppercase Schema Dart Uppercase Types Dart

96
00:07:26,250 --> 00:07:28,770
Object ID this one too.

97
00:07:28,800 --> 00:07:29,940
You'll be good to go.

98
00:07:29,970 --> 00:07:33,960
For the meantime, let's take to the string.

99
00:07:34,080 --> 00:07:36,720
Going to be pretty easy for us, understand?

100
00:07:37,200 --> 00:07:38,280
All right, good.

101
00:07:38,280 --> 00:07:44,460
So now we have an identity that represents the user who is trying to create this post.

102
00:07:44,760 --> 00:07:45,300
Good.

103
00:07:45,330 --> 00:07:48,140
The next one is we need to go to the users.

104
00:07:48,150 --> 00:07:48,930
Failed.

105
00:07:48,960 --> 00:07:50,160
Here comes.

106
00:07:50,310 --> 00:07:57,300
We need to provide a field that's going to hold all the posts this user is going to create.

107
00:07:57,630 --> 00:08:05,940
So here we're going to provide a field called posts, meaning many posts, and then we provide the type

108
00:08:05,940 --> 00:08:08,010
as angry as that.

109
00:08:08,010 --> 00:08:15,180
Or we can make use of this and pass in string of arrays if we want to take object going to be objects

110
00:08:15,180 --> 00:08:16,560
of service as that.

111
00:08:16,580 --> 00:08:23,310
So this one is the same as an array and that is it for this field.

112
00:08:23,310 --> 00:08:30,240
There is no need for us to make it straight meaning required because upon registration do you provide

113
00:08:30,240 --> 00:08:32,130
the post that you are creating?

114
00:08:32,159 --> 00:08:34,020
The answer is no.

115
00:08:34,049 --> 00:08:39,350
Now we are done with the data modeling, how easy it is.

116
00:08:39,360 --> 00:08:48,390
So let's go ahead and create a new user or we can use the same user and create a new post.

117
00:08:48,390 --> 00:08:56,550
But guys remember, any time you make changes to your model or schema, we need to trust your database.

118
00:08:56,700 --> 00:09:02,700
So now back to our compass here or inside visual seed code.

119
00:09:02,700 --> 00:09:11,280
Let's go ahead and then delete the tables that we have or the documents that is the database name or

120
00:09:11,280 --> 00:09:11,970
the documents.

121
00:09:11,970 --> 00:09:14,730
So let's wait for connection to be connected.

122
00:09:14,880 --> 00:09:21,990
So on the block app we have this collection right click and then drop collection and let's type the

123
00:09:21,990 --> 00:09:23,580
name of the collection.

124
00:09:23,580 --> 00:09:26,730
Now I have drop it likewise uses.

125
00:09:26,730 --> 00:09:31,650
I also want to drop the users as that here we go.

126
00:09:31,650 --> 00:09:38,580
Now let's go to ten clients and let's make a request to register a new user.

127
00:09:38,580 --> 00:09:40,650
So let me click on that.

128
00:09:40,650 --> 00:09:50,730
The same request as before hit send and I have one user and on the post to let me hit create here and

129
00:09:50,730 --> 00:09:52,140
I have one post.

130
00:09:52,140 --> 00:09:57,390
Oh I got an error which says that now here is a beauty of it.

131
00:09:57,630 --> 00:10:00,180
It could see that I got an error.

132
00:10:00,180 --> 00:10:03,300
We need that the author field is required.

133
00:10:03,300 --> 00:10:07,440
Meaning that when creating a post you need to provide.

134
00:10:07,580 --> 00:10:08,030
Right.

135
00:10:08,060 --> 00:10:09,890
The user meaning the author.

136
00:10:09,920 --> 00:10:12,780
That is what we have specified here.

137
00:10:12,800 --> 00:10:18,610
So now there is going to be a connection between user and then post.

138
00:10:18,620 --> 00:10:20,180
So how are we going to do?

139
00:10:20,180 --> 00:10:21,590
It is pretty simple.

140
00:10:21,620 --> 00:10:26,990
We need to find the ID of the user, so let's copy any of these IDs.

141
00:10:26,990 --> 00:10:32,020
So when creating the post, we need to provide the user that is the author.

142
00:10:32,030 --> 00:10:40,130
So here are going to be the author and then column the ID and what application we are going to get the

143
00:10:40,130 --> 00:10:43,060
ID from the login user.

144
00:10:43,580 --> 00:10:49,330
But for the meantime, let's do it manually here by copying the ID and paste it here.

145
00:10:49,340 --> 00:10:52,820
So after this one, we are not done yet.

146
00:10:52,850 --> 00:10:58,030
Again, when I hit send, well I still get the author.

147
00:10:58,310 --> 00:10:59,510
It is required.

148
00:10:59,660 --> 00:11:08,480
It's because inside the root we are not handling the request and this is the route that we need to make

149
00:11:08,480 --> 00:11:17,660
sure that a data is going to be saved into the user's array and then we pass in who is creating this

150
00:11:17,660 --> 00:11:18,260
post.

151
00:11:18,470 --> 00:11:18,980
All right.

152
00:11:18,980 --> 00:11:21,650
Let's get into that in the next video.

