1
00:00:03,680 --> 00:00:07,560
In this exercise, we're going to continue working on

2
00:00:07,560 --> 00:00:11,190
the node Mongoose server that we developed in the previous exercise.

3
00:00:11,190 --> 00:00:13,770
We will do another variation of that.

4
00:00:13,770 --> 00:00:19,190
In addition, we will also look at how Mongoose supports sub-documents,

5
00:00:19,190 --> 00:00:22,760
and this enables us to store a document inside

6
00:00:22,760 --> 00:00:26,860
a document for storing additional information.

7
00:00:26,860 --> 00:00:31,055
Let's look at that in the exercise next.

8
00:00:31,055 --> 00:00:36,905
Continuing with the example that we have been working on, now,

9
00:00:36,905 --> 00:00:39,680
in the index.js file,

10
00:00:39,680 --> 00:00:46,040
now we'll see a new way of creating a dish and adding it into our application.

11
00:00:46,040 --> 00:00:51,590
So, earlier we had done var newDish and dishes and define the dish.

12
00:00:51,590 --> 00:00:57,980
Now, in this case we will use a new method called dishes create which takes,

13
00:00:57,980 --> 00:01:05,950
as a parameter, the newDish to create and then saves it in our application.

14
00:01:05,950 --> 00:01:11,165
So, instead of doing this newDish save,

15
00:01:11,165 --> 00:01:18,550
we can simply remove that and directly deal with the dishes create method here.

16
00:01:18,550 --> 00:01:22,850
So, the dishes create method will take as a parameter

17
00:01:22,850 --> 00:01:27,320
a new document that needs to be stored in our database,

18
00:01:27,320 --> 00:01:31,355
and then creates and adds the document to the database.

19
00:01:31,355 --> 00:01:35,285
Thereafter, we can continue with the rest of the exercise.

20
00:01:35,285 --> 00:01:40,390
This minor modification will show

21
00:01:40,390 --> 00:01:45,790
that the same application runs exactly the same way as before.

22
00:01:45,790 --> 00:01:47,365
So, with this change,

23
00:01:47,365 --> 00:01:50,080
let me just change the indentation

24
00:01:50,080 --> 00:01:56,370
here so that the chaining of the then methods is very clear.

25
00:01:56,370 --> 00:02:01,130
So, we first do a dishes create which will return a promise,

26
00:02:01,130 --> 00:02:04,300
and then change to the then and then we will carry out

27
00:02:04,300 --> 00:02:10,255
the remaining steps on the created dish.

28
00:02:10,255 --> 00:02:15,905
Let's save the changes and then go and take a look at this version of our server.

29
00:02:15,905 --> 00:02:17,475
Going to the terminal,

30
00:02:17,475 --> 00:02:20,675
once again, running the application,

31
00:02:20,675 --> 00:02:28,040
we see that it will create exactly the same as the previous variation of the server.

32
00:02:28,040 --> 00:02:33,290
So, we are creating a new dish here and then inserting it into

33
00:02:33,290 --> 00:02:36,800
the database and thereafter we

34
00:02:36,800 --> 00:02:40,760
are retrieving that dish and then printing out on the screen.

35
00:02:40,760 --> 00:02:43,610
With this change, this is a good time for us to do

36
00:02:43,610 --> 00:02:47,795
a git commit with the message Mongoose part two.

37
00:02:47,795 --> 00:02:57,450
So, let's check the git status and then add the file to the repository,

38
00:02:57,450 --> 00:03:06,390
and then do a git commit with the message Mongoose part two.

39
00:03:06,390 --> 00:03:08,800
Continuing with the exercise,

40
00:03:08,800 --> 00:03:13,910
in the next step we're going to add in a sub-document

41
00:03:13,910 --> 00:03:19,385
to the document and see how sub-documents can be stored in our database.

42
00:03:19,385 --> 00:03:22,385
So, going to dishes.js file,

43
00:03:22,385 --> 00:03:30,135
I'm going to add in another schema into my dishes.js file called

44
00:03:30,135 --> 00:03:37,360
as comment Schema and

45
00:03:37,360 --> 00:03:42,230
then define this as new Schema.

46
00:03:42,320 --> 00:03:44,880
In this comment schema,

47
00:03:44,880 --> 00:03:46,895
as you would have already expected,

48
00:03:46,895 --> 00:03:51,360
we're going to use this to store comments about the dish.

49
00:03:51,360 --> 00:03:56,380
So, the comments will contain a rating field which

50
00:03:56,380 --> 00:04:02,930
is of the type number,

51
00:04:02,930 --> 00:04:07,250
and its minimum value is one,

52
00:04:07,250 --> 00:04:15,290
maximum value is five and required is true.

53
00:04:15,290 --> 00:04:20,120
So, you can see that when you use a field with the type number,

54
00:04:20,120 --> 00:04:26,085
you can specify the boundaries for it if you choose to.

55
00:04:26,085 --> 00:04:35,640
Then the next field is a comment field which would be of the type string.

56
00:04:35,640 --> 00:04:41,780
So, this stores a message and then required

57
00:04:41,780 --> 00:04:49,300
is true and then we'll also add another field called author,

58
00:04:51,220 --> 00:05:01,120
which is of the type string and required is true.

59
00:05:01,120 --> 00:05:07,600
We will also add the timestamp field for this,

60
00:05:07,600 --> 00:05:11,875
and set that to true.

61
00:05:11,875 --> 00:05:14,570
So, for every comment that is added,

62
00:05:14,570 --> 00:05:17,570
we will also store the corresponding timestamp.

63
00:05:17,570 --> 00:05:20,990
So, now that we have declared a comment schema,

64
00:05:20,990 --> 00:05:27,465
we can then go ahead and modify the dish schema to be able to store comments.

65
00:05:27,465 --> 00:05:29,745
So, within the dishes schema,

66
00:05:29,745 --> 00:05:37,370
we can add in comments

67
00:05:37,370 --> 00:05:44,750
which is an array of the type comment schema,

68
00:05:44,750 --> 00:05:49,705
so which means that every dish object,

69
00:05:49,705 --> 00:05:57,860
dish document can have multiple comments stored within an array inside the dish document.

70
00:05:57,860 --> 00:06:04,830
So, this is the comment documents becomes sub-documents inside a dish document.

71
00:06:04,830 --> 00:06:08,524
So, we're storing all the comments about the dish inside

72
00:06:08,524 --> 00:06:13,765
the dish itself as an array of comment documents.

73
00:06:13,765 --> 00:06:20,825
So, this is the usage of the sub-document in Mongoose.

74
00:06:20,825 --> 00:06:28,395
After this, let's go to the index.js file and then modify our index.js file.

75
00:06:28,395 --> 00:06:32,640
In here, they create the dish just like before.

76
00:06:32,640 --> 00:06:35,010
Then after we create the dish,

77
00:06:35,010 --> 00:06:38,715
then we console log the dish.

78
00:06:38,715 --> 00:06:43,080
Thereafter, we will update the dish.

79
00:06:43,080 --> 00:06:49,925
So, we'll say dishes find by ID and update.

80
00:06:49,925 --> 00:06:55,345
So, we're going to modify the dish that we just inserted in the previous step.

81
00:06:55,345 --> 00:06:58,690
So, we'll say, "Find by ID and update."

82
00:06:58,690 --> 00:07:01,930
And we're going to supply.

83
00:07:01,930 --> 00:07:05,305
In this takes two parameters,

84
00:07:05,305 --> 00:07:10,045
the first parameter is the dish._id.

85
00:07:10,045 --> 00:07:15,205
Now, this dish._id of course refers to this dish that we have just inserted,

86
00:07:15,205 --> 00:07:19,895
and then we're going to modify that same dish in the next step.

87
00:07:19,895 --> 00:07:22,420
So, we'll say, "Dish._id." And then,

88
00:07:22,420 --> 00:07:34,610
within brackets, we specify $set, description,

89
00:07:36,600 --> 00:07:45,580
updated test and new true.

90
00:07:45,580 --> 00:07:49,240
So, this, we are modifying the dish that we have

91
00:07:49,240 --> 00:07:54,865
just inserted by updating its description there.

92
00:07:54,865 --> 00:07:58,735
Now, this has to be enclosed inside.

93
00:07:58,735 --> 00:08:01,310
This second flag that we are supplying here,

94
00:08:01,310 --> 00:08:03,985
new colon equal to true,

95
00:08:03,985 --> 00:08:07,895
means that once the update of the dish is complete,

96
00:08:07,895 --> 00:08:11,075
then this will return the dish,

97
00:08:11,075 --> 00:08:14,530
updated dish back to us.

98
00:08:14,530 --> 00:08:20,350
So, that's the reason for using this flag here, say, new true.

99
00:08:20,350 --> 00:08:25,275
So, this dish will be returned in the next step.

100
00:08:25,275 --> 00:08:32,905
And so, we're going to take that dish and then print out that dish.

101
00:08:32,905 --> 00:08:34,540
So, in the next step,

102
00:08:34,540 --> 00:08:41,845
we're going to modify this and say, "This, then dish."

103
00:08:41,845 --> 00:08:45,790
And then, we'll do a console log of dish.

104
00:08:45,790 --> 00:08:55,060
Then after this, we're going to insert a comment into the dish.

105
00:08:55,060 --> 00:09:00,405
So, we'll say, "Dish.comments.push."

106
00:09:00,405 --> 00:09:04,420
Recall that the comments is a field inside the dish.

107
00:09:04,420 --> 00:09:06,930
So, going back to the schema,

108
00:09:06,930 --> 00:09:12,685
we see that this comments is an array of the commentSchema type.

109
00:09:12,685 --> 00:09:16,360
So, which means that we can push an item into this array.

110
00:09:16,360 --> 00:09:17,700
So, going back here,

111
00:09:17,700 --> 00:09:21,180
we'll say, "Dish comments push."

112
00:09:21,180 --> 00:09:27,380
And then, we'll push a comment document into the dish here.

113
00:09:27,380 --> 00:09:31,415
So, I will say, "Dish comments push."

114
00:09:31,415 --> 00:09:33,990
And specify the fields here.

115
00:09:33,990 --> 00:10:00,540
So, we'll say, "Rating five, comment and author."

116
00:10:07,890 --> 00:10:17,155
And so, this will push this comment into the comment field of this specific dish.

117
00:10:17,155 --> 00:10:20,065
So, now that we have modified the dish,

118
00:10:20,065 --> 00:10:24,655
the next step that we're going to do is to,

119
00:10:24,655 --> 00:10:30,030
let me close off this and then put that into it, then there.

120
00:10:30,030 --> 00:10:32,400
I'm going to come back and fix that in a short while.

121
00:10:32,400 --> 00:10:34,555
So, now that we have modified the dish,

122
00:10:34,555 --> 00:10:37,630
we need to save the dish.

123
00:10:37,630 --> 00:10:42,430
So, we'll say, "Return dish safe."

124
00:10:42,430 --> 00:10:44,280
So, in this step,

125
00:10:44,280 --> 00:10:47,520
we are inserting a comment into the dish,

126
00:10:47,520 --> 00:10:51,055
the dish that we have just created and updated,

127
00:10:51,055 --> 00:10:55,360
and then we are saving that comment;

128
00:10:55,360 --> 00:11:00,350
and then, this will return the dish.

129
00:11:00,350 --> 00:11:07,480
And when the dish is returned in the next callback,

130
00:11:07,480 --> 00:11:15,160
I'm going to then say, "Console log dish."

131
00:11:15,160 --> 00:11:16,240
And then, after that,

132
00:11:16,240 --> 00:11:17,920
we will say, "Return."

133
00:11:17,920 --> 00:11:22,435
So, with this, we have modified our index.js file.

134
00:11:22,435 --> 00:11:27,265
Let's save the changes and look at this application.

135
00:11:27,265 --> 00:11:29,135
Going to the terminal,

136
00:11:29,135 --> 00:11:34,770
let's run npm start and then see the resulting value here.

137
00:11:34,770 --> 00:11:38,570
So, going back, we see that in the first step,

138
00:11:38,570 --> 00:11:45,430
we have created this dish here with the name and description as we have given earlier.

139
00:11:45,430 --> 00:11:49,675
And then, once the dish is created,

140
00:11:49,675 --> 00:11:50,980
in the second step,

141
00:11:50,980 --> 00:11:55,730
we updated the dish and then printed out the updated version of the dish.

142
00:11:55,730 --> 00:11:59,325
So, the updated version of the dish is printed out right there.

143
00:11:59,325 --> 00:12:03,940
And you can see that the description has been updated from the previous case,

144
00:12:03,940 --> 00:12:10,210
but the id remains exactly the same as the dish that we created earlier.

145
00:12:10,210 --> 00:12:11,720
And the comments field,

146
00:12:11,720 --> 00:12:12,875
as you can see here,

147
00:12:12,875 --> 00:12:15,150
is an empty array and the smoked.

148
00:12:15,150 --> 00:12:22,555
In third step, we push the comment into our dish here;

149
00:12:22,555 --> 00:12:25,560
and then so, this particular comment that

150
00:12:25,560 --> 00:12:28,880
we added has been added into this comments array.

151
00:12:28,880 --> 00:12:32,475
So, you can see that comment being enclosed here,

152
00:12:32,475 --> 00:12:39,440
and this is the updated dish that has now been printed onto the screen here.

153
00:12:39,440 --> 00:12:45,270
So, you can see that we have done several steps one after another with our dish.

154
00:12:45,270 --> 00:12:46,840
We first created the dish,

155
00:12:46,840 --> 00:12:48,460
then we updated the dish,

156
00:12:48,460 --> 00:12:51,340
then we inserted a comment into the dish,

157
00:12:51,340 --> 00:12:55,745
which is a sub-document inside the dish document;

158
00:12:55,745 --> 00:12:59,035
and then, we printed out the result to you.

159
00:12:59,035 --> 00:13:02,390
With this, we complete this exercise.

160
00:13:02,390 --> 00:13:06,500
In this exercise, we have seen how we can,

161
00:13:06,500 --> 00:13:10,930
first, create a dish by using dishes create,

162
00:13:10,930 --> 00:13:14,520
then we saw the use of sub-documents: there,

163
00:13:14,520 --> 00:13:18,060
we defined a schema within the dishes.js

164
00:13:18,060 --> 00:13:21,720
file and then use the comment schema that we defined and

165
00:13:21,720 --> 00:13:29,130
then defined the field in the dishes schema as an array of the commentSchema type.

166
00:13:29,130 --> 00:13:32,810
And then, in the index.js file,

167
00:13:32,810 --> 00:13:35,445
we were able to create a dish,

168
00:13:35,445 --> 00:13:38,210
update the dish and also push

169
00:13:38,210 --> 00:13:44,140
a comment document into the dish and then save the changes here.

170
00:13:44,140 --> 00:13:46,480
With this, we complete this exercise.

171
00:13:46,480 --> 00:13:54,170
This is a good time for you to do a Git Kermit with the message, "Mongoose part three."