﻿1
00:00:01,090 --> 00:00:03,130
‫So in the last lecture, we learned about

2
00:00:03,130 --> 00:00:06,010
‫Mongoose schemas and models,

3
00:00:06,010 --> 00:00:08,260
‫and so let's now implement a very simple

4
00:00:08,260 --> 00:00:11,173
‫schema and model for our application.

5
00:00:12,920 --> 00:00:14,990
‫And for now, let's do that right here

6
00:00:14,990 --> 00:00:17,330
‫in our server.js file.

7
00:00:17,330 --> 00:00:19,020
‫Later, of course, we're gonna move

8
00:00:19,020 --> 00:00:21,370
‫that into a different file.

9
00:00:21,370 --> 00:00:24,560
‫So, Mongoose is all about models,

10
00:00:24,560 --> 00:00:26,960
‫and a model is like a blueprint

11
00:00:26,960 --> 00:00:29,420
‫that we use to create documents.

12
00:00:29,420 --> 00:00:32,290
‫So it's a bit like classes in JavaScript,

13
00:00:32,290 --> 00:00:35,080
‫which we also kind of use as blueprints

14
00:00:35,080 --> 00:00:38,590
‫in order to create objects out of them, right?

15
00:00:38,590 --> 00:00:41,500
‫So again, we create a model in Mongoose

16
00:00:41,500 --> 00:00:44,000
‫in order to create documents using it,

17
00:00:44,000 --> 00:00:47,670
‫and also to query update and delete these documents.

18
00:00:47,670 --> 00:00:50,840
‫So basically, to perform each of the CRUD operation,

19
00:00:50,840 --> 00:00:53,420
‫so create, read, update, and delete,

20
00:00:53,420 --> 00:00:56,520
‫we need a Mongoose model, and in order to

21
00:00:56,520 --> 00:00:59,520
‫create a model, we actually need a schema.

22
00:00:59,520 --> 00:01:01,700
‫So, we actually create models out of

23
00:01:01,700 --> 00:01:03,820
‫Mongoose schema just like we learned

24
00:01:03,820 --> 00:01:06,490
‫in the last video, and we use the schema

25
00:01:06,490 --> 00:01:09,910
‫to describe our data, to set default values,

26
00:01:09,910 --> 00:01:13,660
‫to validate the data, and all kinds of stuff like that.

27
00:01:13,660 --> 00:01:15,620
‫So just as you will see right now,

28
00:01:15,620 --> 00:01:17,930
‫because now we're actually gonna start creating

29
00:01:17,930 --> 00:01:20,990
‫a very simple schema for our tours.

30
00:01:20,990 --> 00:01:24,980
‫So, let's say tourSchema

31
00:01:28,300 --> 00:01:32,363
‫is a new mongoose.Schema.

32
00:01:33,852 --> 00:01:36,752
‫Add that in here we actually pass our schema as an object.

33
00:01:37,590 --> 00:01:40,990
‫All right, we can then also pass in some options

34
00:01:40,990 --> 00:01:44,090
‫into the schema, but I'm leaving that to a future video

35
00:01:44,090 --> 00:01:46,640
‫where we actually need that feature.

36
00:01:46,640 --> 00:01:49,020
‫So, remember the tour documents that we used

37
00:01:49,020 --> 00:01:51,090
‫to create in the last section when

38
00:01:51,090 --> 00:01:53,190
‫we started exploring Mongoose DB?

39
00:01:53,190 --> 00:01:54,760
‫So we always gave them a name,

40
00:01:54,760 --> 00:01:57,100
‫a rating and a price, right,

41
00:01:57,100 --> 00:01:59,226
‫and so let's now replicate that here

42
00:01:59,226 --> 00:02:02,010
‫by saying name, and then describing

43
00:02:02,010 --> 00:02:04,717
‫what type of data we want after name,

44
00:02:04,717 --> 00:02:07,500
‫and we want it to be a string.

45
00:02:07,500 --> 00:02:09,770
‫So Mongoose actually uses the native

46
00:02:09,770 --> 00:02:13,280
‫JavaScript data types, and so here we can say string,

47
00:02:13,280 --> 00:02:16,980
‫or after rating we can say that we want a number

48
00:02:19,394 --> 00:02:22,163
‫and the same as the price.

49
00:02:23,340 --> 00:02:25,890
‫It should also be a number, right?

50
00:02:25,890 --> 00:02:30,320
‫So this is the most basic way of describing our data.

51
00:02:30,320 --> 00:02:33,130
‫So we have a name, a rating, and a price,

52
00:02:33,130 --> 00:02:34,913
‫and we specified the data type

53
00:02:34,913 --> 00:02:38,150
‫that we expect for each of these fields.

54
00:02:38,150 --> 00:02:40,340
‫So again, this is the most basic way

55
00:02:40,340 --> 00:02:43,100
‫of describing a schema, but we can take it

56
00:02:43,100 --> 00:02:45,650
‫one step further by defining something called

57
00:02:45,650 --> 00:02:48,360
‫schema type options for each field,

58
00:02:48,360 --> 00:02:50,600
‫or only for some specific field.

59
00:02:50,600 --> 00:02:52,640
‫So let's start here with the name,

60
00:02:52,640 --> 00:02:54,270
‫and instead of just specifying it

61
00:02:54,270 --> 00:02:58,490
‫as a string, let's actually pass in another object.

62
00:02:58,490 --> 00:03:01,340
‫And so now, we say that we want the type

63
00:03:01,340 --> 00:03:02,850
‫to be string but we can now

64
00:03:02,850 --> 00:03:05,640
‫define a couple more options, okay?

65
00:03:05,640 --> 00:03:10,640
‫For example, we can say that this field is required,

66
00:03:12,070 --> 00:03:13,060
‫and so we simply set

67
00:03:13,060 --> 00:03:17,110
‫the required property here to true, all right?

68
00:03:17,110 --> 00:03:20,970
‫So again, this object here are the schema type options,

69
00:03:20,970 --> 00:03:23,320
‫and they can be different for different types,

70
00:03:23,320 --> 00:03:25,640
‫for example the number type has some different

71
00:03:25,640 --> 00:03:28,250
‫schema options than the string here,

72
00:03:28,250 --> 00:03:30,770
‫but many of them are also similar.

73
00:03:30,770 --> 00:03:32,630
‫So for example, the required, we can use it

74
00:03:32,630 --> 00:03:35,403
‫on number as well, and so let's do that here.

75
00:03:39,750 --> 00:03:42,776
‫So the type, we still want it to be a number,

76
00:03:42,776 --> 00:03:47,460
‫and we also want it to be required, okay?

77
00:03:47,460 --> 00:03:49,460
‫Now, here in the required we can actually

78
00:03:49,460 --> 00:03:52,310
‫specify the error that we want to be displayed

79
00:03:52,310 --> 00:03:54,020
‫when we're missing this field.

80
00:03:54,020 --> 00:03:55,760
‫So, in order to do that we just have to

81
00:03:55,760 --> 00:03:59,300
‫pass in an array, and the first one is true.

82
00:03:59,300 --> 00:04:01,720
‫So the first element is true, and the second one

83
00:04:01,720 --> 00:04:03,820
‫is the error string.

84
00:04:03,820 --> 00:04:08,670
‫Let's say, a tour must have a name,

85
00:04:08,670 --> 00:04:10,270
‫and then here let's do the same.

86
00:04:15,459 --> 00:04:17,490
‫The tour must have a price, okay?

87
00:04:17,490 --> 00:04:19,698
‫Then we can also set default values

88
00:04:19,698 --> 00:04:22,720
‫and let's do that here for the rating.

89
00:04:22,720 --> 00:04:24,640
‫So again, I'm gonna specify some

90
00:04:24,640 --> 00:04:27,610
‫schema type options here, which is not mandatory,

91
00:04:27,610 --> 00:04:29,700
‫so it's enough to simply define

92
00:04:29,700 --> 00:04:32,210
‫the type here like we did, but if you want

93
00:04:32,210 --> 00:04:34,150
‫some more features, then we need to to at least

94
00:04:34,150 --> 00:04:37,033
‫specify the schema type options object.

95
00:04:38,080 --> 00:04:42,580
‫Okay, and then we need to specify the type again,

96
00:04:42,580 --> 00:04:47,480
‫which is number, and as a default, I want it to be 4.5.

97
00:04:48,350 --> 00:04:50,990
‫And so if we'd now create a new tour document

98
00:04:50,990 --> 00:04:54,270
‫using this schema and not specifying the rating,

99
00:04:54,270 --> 00:04:58,928
‫it would then automatically be set to 4.5, all right?

100
00:04:58,928 --> 00:05:02,100
‫And just to finish, let's try another one here,

101
00:05:02,100 --> 00:05:04,853
‫which is to say that the name should be unique.

102
00:05:05,910 --> 00:05:09,080
‫So, unique and set it to true,

103
00:05:09,080 --> 00:05:11,020
‫and so like this, we can now have two

104
00:05:11,020 --> 00:05:14,390
‫tour documents with the same name, all right?

105
00:05:14,390 --> 00:05:16,550
‫So, this is our very basic schema,

106
00:05:16,550 --> 00:05:18,370
‫let's now go ahead and actually

107
00:05:18,370 --> 00:05:21,230
‫create a model out of it, all right?

108
00:05:21,230 --> 00:05:22,380
‫And that's very simple,

109
00:05:24,890 --> 00:05:28,412
‫we simply create a variable called Tour,

110
00:05:28,412 --> 00:05:32,390
‫and then mongoose.model,

111
00:05:32,390 --> 00:05:36,380
‫then the name of the model, which is Tour with an uppercase

112
00:05:36,380 --> 00:05:37,780
‫T here, all right.

113
00:05:37,780 --> 00:05:39,930
‫And so that's just kind of a convention

114
00:05:39,930 --> 00:05:42,500
‫in programming to always use uppercase

115
00:05:42,500 --> 00:05:44,720
‫on model names and variables.

116
00:05:44,720 --> 00:05:47,040
‫So here it's the same, so I also created

117
00:05:47,040 --> 00:05:49,980
‫this tour variable here with a capital T

118
00:05:49,980 --> 00:05:52,630
‫just so we know that we're dealing with a model here.

119
00:05:53,750 --> 00:05:55,940
‫All right, so the name of the model

120
00:05:55,940 --> 00:06:00,940
‫and then the schema, and that's it.

121
00:06:01,310 --> 00:06:04,210
‫So just like this, we created a tour

122
00:06:04,210 --> 00:06:08,210
‫out of the schema that we created here in the beginning.

123
00:06:08,210 --> 00:06:10,150
‫All right, and so in the next video

124
00:06:10,150 --> 00:06:12,570
‫we're gonna then use this model here

125
00:06:12,570 --> 00:06:17,220
‫in order to create our very first tour document, all right?

126
00:06:17,220 --> 00:06:21,300
‫So just a recap here, we used new mongoose.Schema here

127
00:06:21,300 --> 00:06:24,525
‫to specify a schema for our data.

128
00:06:24,525 --> 00:06:27,870
‫So, basically describing it and also doing some validation.

129
00:06:27,870 --> 00:06:29,800
‫For example, this required here,

130
00:06:29,800 --> 00:06:32,580
‫this is actually something called a validator

131
00:06:32,580 --> 00:06:35,120
‫because it is used to validate our data.

132
00:06:35,120 --> 00:06:37,330
‫In this case, simply to validate

133
00:06:37,330 --> 00:06:39,390
‫if the name is actually there.

134
00:06:39,390 --> 00:06:41,757
‫And there are a lot of validators in Mongoose,

135
00:06:41,757 --> 00:06:44,200
‫and we can actually also create our own,

136
00:06:44,200 --> 00:06:46,400
‫and so of course we're gonna talk about that

137
00:06:46,400 --> 00:06:50,186
‫a bit later, again, once we actually need these features.

138
00:06:50,186 --> 00:06:52,600
‫Anyway, let's now quickly move on

139
00:06:52,600 --> 00:06:54,540
‫in order to finally start using

140
00:06:54,540 --> 00:06:56,113
‫the model that we just created.

