WEBVTT

1
00:01.040 --> 00:11.930
In the previous video, I already showed you how to connect with the MongoDB, and in this video,

2
00:11.960 --> 00:15.950
let's create the some schema.

3
00:16.250 --> 00:25.220
Or we can go and create a document collection inside the MongoDB database.

4
00:25.880 --> 00:29.600
Now let's show you how we can do that.

5
00:29.720 --> 00:37.970
Firstly, we will define the folder called schema and we will create a schema like this.

6
00:38.210 --> 00:41.120
In this example you will cat.schema.

7
00:41.150 --> 00:43.790
.ts

8
00:43.790 --> 00:48.110
So I will show you how we can do similar like that in the post.

9
00:48.140 --> 00:51.560
Over here I will create a new folder called schemas.

10
00:51.980 --> 01:03.230
Sorry schemas inside schemas for I will create a post.schema.ts

11
01:03.800 --> 01:05.750
Okay that the first one

12
01:06.470 --> 01:19.340
And in order to create the schema, because we use the ORM or we can call ORD

13
01:22.400 --> 01:23.300
right

14
01:26.330 --> 01:31.520
So for that reason we will write the object object relational mapping.

15
01:31.640 --> 01:36.410
We will mapping from a class into the real database.

16
01:36.440 --> 01:42.710
And for that reason we will create a class to representation of the document.

17
01:47.630 --> 01:54.890
So firstly, we will define a class and use the @Schema.

18
01:55.010 --> 01:59.000
To make it will be the schema of the database.

19
01:59.420 --> 02:02.180
And we use the prop property right here.

20
02:03.380 --> 02:06.410
So this will be the column, right.

21
02:07.850 --> 02:10.700
And now instead of

22
02:11.630 --> 02:14.750
try to understand anything right here.

23
02:14.780 --> 02:16.070
I will copy

24
02:16.340 --> 02:20.270
paste and see what is the result.

25
02:21.260 --> 02:26.330
Okay, let me copy everything, and paste it and firstly I will replace.

26
02:28.580 --> 02:30.320
Firstly, I will

27
02:33.140 --> 02:35.000
I will change the name.

28
02:35.420 --> 02:35.840
Okay.

29
02:36.290 --> 02:41.480
From the cat schema and cat document into the post document.

30
02:42.470 --> 02:43.310
Okay, Post.

31
02:43.310 --> 02:43.970
Document.

32
02:44.030 --> 02:44.570
Post

33
02:44.690 --> 02:46.100
Here and now.

34
02:46.100 --> 02:49.850
You don't need to worry about what is inside here.

35
02:50.060 --> 02:52.490
What is HydratedDocument ?

36
02:52.880 --> 02:53.390
Okay.

37
02:53.420 --> 02:55.760
You don't need to

38
02:58.100 --> 02:59.240
You don't need to know.

39
02:59.270 --> 03:03.050
All the that, this is not important.

40
03:03.260 --> 03:06.170
The important is make the code working.

41
03:06.440 --> 03:10.310
This will be good with the post.

42
03:10.640 --> 03:12.890
I need, I need the title.

43
03:14.480 --> 03:16.850
And with the that title, we need the string.

44
03:16.880 --> 03:17.510
Right?

45
03:17.990 --> 03:23.060
And the description will be string also.

46
03:24.290 --> 03:26.270
And I will remove everything right here.

47
03:27.410 --> 03:27.860
Okay.

48
03:27.890 --> 03:30.260
That's for schema.

49
03:31.700 --> 03:37.070
And now let me open the terminal a little bit right here.

50
03:38.060 --> 03:40.820
And I will scroll down a little bit.

51
03:41.270 --> 03:43.100
We can see, with prop.

52
03:43.130 --> 03:46.730
We can mark this with data type like this.

53
03:47.180 --> 03:54.440
Or we can use the validation I will show you how to validation later.

54
03:54.470 --> 04:00.110
Don't worry that for now we can reference this with different.

55
04:02.840 --> 04:05.900
Document different collection.

56
04:09.230 --> 04:11.660
Now we scroll down a little bit.

57
04:16.070 --> 04:16.910
Right here.

58
04:17.360 --> 04:24.200
The code right here is what we use inside the Express.js framework, right?

59
04:26.540 --> 04:29.330
With the Expressjs Framework, we use something like this.

60
04:29.450 --> 04:36.800
But with NestJS, we will prefer the class approach.

61
04:36.830 --> 04:37.460
Way

62
04:39.050 --> 04:50.840
And now, let's go to the module and import the MongooseModule.forFeature and define something like

63
04:50.870 --> 04:55.100
the name of the database and the schema.

64
04:55.790 --> 05:00.020
Let me show you how we can do that. Inside the post module right here.

65
05:00.530 --> 05:04.520
I will copy okay and never remember the code over here.

66
05:05.240 --> 05:10.790
I will copy until the array right here and inside the import.

67
05:11.180 --> 05:13.250
We don't have import here, right?

68
05:14.240 --> 05:14.750
Import.

69
05:18.950 --> 05:23.570
And instead of import from the Cat I will use Post.

70
05:25.950 --> 05:26.430
Right.

71
05:27.390 --> 05:27.930
Oh, sorry.

72
05:27.930 --> 05:29.820
The Post is not from "nestjs/common"

73
05:30.960 --> 05:34.620
The Post comes from the schema.

74
05:34.650 --> 05:35.670
The schema.

75
05:35.970 --> 05:38.700
And I will import the PostSchema.

76
05:41.400 --> 05:42.090
Now.

77
05:45.690 --> 05:47.670
And make sure you a comma.

78
05:48.270 --> 05:49.080
It's a little bit.

79
05:49.590 --> 05:51.720
And the post right here.

80
05:52.230 --> 05:54.840
It will be the class, right?

81
05:56.340 --> 05:59.790
And the schema here is in the schema.

82
05:59.790 --> 06:02.760
We already export right here.

83
06:05.220 --> 06:14.370
And because this schema right here belongs to the post module.

84
06:14.580 --> 06:17.490
So we will import this inside the post module.

85
06:19.080 --> 06:24.240
And that's it, let me go to the MongoDB.

86
06:25.350 --> 06:27.930
And click inside the cluster.

87
06:29.970 --> 06:33.150
And click on the browse collection.

88
06:36.600 --> 06:38.250
Inside the collection right here.

89
06:39.600 --> 06:43.500
Now, you can see we have a database and a posts right here.

90
06:43.860 --> 06:44.460
Right.

91
06:44.610 --> 06:49.110
The database name's default will be "test" because we don't name for it.

92
06:50.280 --> 06:59.790
If I go to the app.module, I will name for it, for example like post_db

93
06:59.970 --> 07:00.540
Okay.

94
07:00.570 --> 07:07.950
And I will drop the test, type "test" to the drop all of that

95
07:11.280 --> 07:18.060
And if I close the project and run it again let's see what happened.

96
07:24.900 --> 07:25.380
Okay.

97
07:25.710 --> 07:33.480
Now we already have a post_db and the posts collection, right?

98
07:34.320 --> 07:34.920
Very cool

99
07:37.770 --> 07:39.870
And that for the video.

100
07:40.860 --> 07:42.750
And let me summarize.

101
07:43.110 --> 07:51.810
In this video, we already understand how we can create a collection and some documentation.

102
07:51.810 --> 07:54.180
We will work with documentation later.

103
07:55.470 --> 08:03.120
Okay, we use the schema and prop to define the property and schema for defined collection, and we

104
08:03.120 --> 08:03.960
will export that.

105
08:04.170 --> 08:13.710
But you don't need to worry about what is the document or the schema factory or .createForClass

106
08:13.890 --> 08:16.740
We don't need to know all that.

107
08:18.180 --> 08:20.580
Okay, this is not important.

108
08:20.730 --> 08:28.050
In later we can use the post document right here for example like the data type.

109
08:29.640 --> 08:32.190
But I will work with that later.

110
08:32.520 --> 08:33.870
Don't worry for now.

111
08:34.680 --> 08:36.660
So that's it for the video.

112
08:36.690 --> 08:38.850
I will see you in the next one.