1
00:00:03,950 --> 00:00:09,370
In this exercise, we will continue with the previous exercise where we

2
00:00:09,370 --> 00:00:14,650
developed the interaction between the node application and the MongoDB server.

3
00:00:14,650 --> 00:00:18,120
In this exercise, I am going to encapsulate a few of

4
00:00:18,120 --> 00:00:21,750
the database operations into a node module of its own,

5
00:00:21,750 --> 00:00:23,540
a file-based node module,

6
00:00:23,540 --> 00:00:29,345
and then make use of it within my node application in order to interact with the server.

7
00:00:29,345 --> 00:00:31,940
At the same time, we'll perform several operations on

8
00:00:31,940 --> 00:00:35,630
the server to demonstrate that we will be able

9
00:00:35,630 --> 00:00:43,040
to interact with the server using our node application and the node MongoDB driver.

10
00:00:43,040 --> 00:00:47,605
To get started, let's go to our project,

11
00:00:47,605 --> 00:00:53,975
and then create a new file named operations.js.

12
00:00:53,975 --> 00:00:57,720
This file will encapsulate all that database operations,

13
00:00:57,720 --> 00:01:00,705
the four operations that I'm going to perform insert,

14
00:01:00,705 --> 00:01:05,855
find, remove and update a document in my database.

15
00:01:05,855 --> 00:01:09,980
Now, this will be organized as a file-based node module,

16
00:01:09,980 --> 00:01:15,070
which then I will make use of it in my node application to access the server.

17
00:01:15,070 --> 00:01:16,330
So, to get started, let

18
00:01:16,330 --> 00:01:25,780
me first require assert,

19
00:01:25,780 --> 00:01:27,565
in this node module,

20
00:01:27,565 --> 00:01:30,225
and since this happens to be a node module,

21
00:01:30,225 --> 00:01:35,055
we'll be exporting several methods from the node module.

22
00:01:35,055 --> 00:01:41,705
The first method would be insert document obviously as you would expect,

23
00:01:41,705 --> 00:01:44,520
and this will take four parameters DB,

24
00:01:44,520 --> 00:01:49,555
the MongoDB, database connection within my node application,

25
00:01:49,555 --> 00:01:54,240
that I will obtain within the node application.

26
00:01:54,240 --> 00:01:57,785
Then the second one is the document which I want to insert.

27
00:01:57,785 --> 00:02:02,800
The third parameter is the collection into which I want to insert the document.

28
00:02:02,800 --> 00:02:05,675
The last one is a callback function,

29
00:02:05,675 --> 00:02:10,425
which will be called back once that operation is completed.

30
00:02:10,425 --> 00:02:14,800
Then let's close the function here,

31
00:02:14,800 --> 00:02:20,100
and this essentially encapsulates the insert document method here.

32
00:02:20,100 --> 00:02:27,415
So, this is a function which is exported by this node module here.

33
00:02:27,415 --> 00:02:34,250
Now, I'm going to also incorporate a few more methods here.

34
00:02:34,250 --> 00:02:42,870
The second one would be finddocuments.

35
00:02:42,870 --> 00:02:46,025
Not just one but several documents.

36
00:02:46,025 --> 00:02:54,390
This I would search the collection and find all the documents that are in the collection.

37
00:02:54,390 --> 00:02:58,130
So, that's why I'm only taking the database

38
00:02:58,130 --> 00:03:02,090
and the collection as the two parameters along with the callback.

39
00:03:02,090 --> 00:03:11,550
The third function that I'm going to implement an export from here is the removedocument.

40
00:03:12,290 --> 00:03:15,590
This supports the delete operation,

41
00:03:15,590 --> 00:03:19,855
and so the name of document will take the database, the document,

42
00:03:19,855 --> 00:03:23,775
the collection and the callback as the four parameters,

43
00:03:23,775 --> 00:03:30,595
and we'll call the callback when the operation is completed.

44
00:03:30,595 --> 00:03:34,445
The final one of course is to update the document.

45
00:03:34,445 --> 00:03:38,120
Now of course, you don't necessarily have to do it this way.

46
00:03:38,120 --> 00:03:42,830
I just felt that this would be another way of illustrating how you can

47
00:03:42,830 --> 00:03:49,110
encapsulate that functions into its own node module.

48
00:03:49,110 --> 00:03:55,395
This is just reorganizing the code in a way that is more easier to use.

49
00:03:55,395 --> 00:03:57,205
So, for the exports,

50
00:03:57,205 --> 00:04:00,070
for the update document it takes the DB,

51
00:04:00,070 --> 00:04:03,750
some way of identifying the document as a second parameter.

52
00:04:03,750 --> 00:04:06,205
The third parameter is the update,

53
00:04:06,205 --> 00:04:08,750
and the fourth parameter is the collection in which

54
00:04:08,750 --> 00:04:11,505
this document exists, and the callback.

55
00:04:11,505 --> 00:04:18,160
So, four functions to be exported by the operations.js file.

56
00:04:18,160 --> 00:04:20,380
Now, within this functions,

57
00:04:20,380 --> 00:04:21,735
let's implement one by one.

58
00:04:21,735 --> 00:04:23,420
In the insert document,

59
00:04:23,420 --> 00:04:27,680
the first thing that I'm going to do is say

60
00:04:27,680 --> 00:04:36,000
const collection and it'll say DB collection,

61
00:04:38,930 --> 00:04:44,710
and the parameter is the collection.

62
00:04:45,350 --> 00:04:50,190
So, we'll look for the collection there.

63
00:04:50,190 --> 00:04:54,640
Now, this has to be performed in all the four methods.

64
00:04:54,640 --> 00:04:59,990
So, I'm going to go and paste this code into all the four methods here,

65
00:05:01,140 --> 00:05:05,390
because that's a function that I need in all of them.

66
00:05:05,390 --> 00:05:07,820
So, once I get hold of the collection,

67
00:05:07,820 --> 00:05:11,680
then as you recall from the previous exercise,

68
00:05:11,680 --> 00:05:13,895
I can perform operations on the collection.

69
00:05:13,895 --> 00:05:19,160
So, I'll say collection, insert,

70
00:05:21,300 --> 00:05:27,610
and this takes as the first parameter the document to be inserted,

71
00:05:27,610 --> 00:05:35,695
and the second parameter is the callback with the error and that result.

72
00:05:35,695 --> 00:05:40,990
So, now when this document

73
00:05:40,990 --> 00:05:46,780
is inserted by calling the insert method on the database collection,

74
00:05:46,780 --> 00:05:49,180
the insert method let me remind you,

75
00:05:49,180 --> 00:05:52,090
is supported by the MongoDB driver.

76
00:05:52,090 --> 00:05:53,980
So, that's the method that we're using here,

77
00:05:53,980 --> 00:05:56,420
or that's the function that we're using here.

78
00:05:56,420 --> 00:06:05,060
Now, the first thing that I'm going to check for this, assertequal err, null.

79
00:06:05,060 --> 00:06:09,185
So, I want to make sure that I don't have an error.

80
00:06:09,185 --> 00:06:11,225
So I want to make sure that the error is null.

81
00:06:11,225 --> 00:06:12,470
If it is not null,

82
00:06:12,470 --> 00:06:15,930
then this will print out the information and then quit the application.

83
00:06:15,930 --> 00:06:19,760
At this moment, I feel that this is okay to handle it that later on,

84
00:06:19,760 --> 00:06:24,680
we'll see how we can have a global way of handling all the errors when we

85
00:06:24,680 --> 00:06:34,390
implement in the next lesson another way of accessing the MongoDB database.

86
00:06:34,390 --> 00:06:36,640
Now, after I do this,

87
00:06:36,640 --> 00:06:38,050
I'm going to implement,

88
00:06:38,050 --> 00:06:40,075
I'm going to simply log this information.

89
00:06:40,075 --> 00:06:48,570
So I'll say, console log and insert it.

90
00:06:48,570 --> 00:06:55,375
So, this information will be printed out to the screen here, inserted.

91
00:06:55,375 --> 00:07:00,440
Now again, remember the semicolons and all that are

92
00:07:00,440 --> 00:07:05,120
important don't forget them when you are typing in your code.

93
00:07:05,120 --> 00:07:07,930
So, I'll say inserted result.

94
00:07:07,930 --> 00:07:12,165
This result object that is returned,

95
00:07:12,165 --> 00:07:15,785
will have on it a property called the result property,

96
00:07:15,785 --> 00:07:20,270
and this property will contain a value.

97
00:07:20,270 --> 00:07:23,600
This result property is also a JavaScript object,

98
00:07:23,600 --> 00:07:31,395
and this will contain a property n which tells us how many documents have been inserted.

99
00:07:31,395 --> 00:07:36,875
So that's the information that I'm going to print out here,

100
00:07:36,875 --> 00:07:39,540
and then we'll go to the next line and

101
00:07:39,540 --> 00:07:50,750
say "documents into the collection."

102
00:07:53,070 --> 00:08:02,040
Now, this is just a way of informing the user that this operation took place correctly,

103
00:08:02,040 --> 00:08:10,135
and then they will pass that result back to our calling function.

104
00:08:10,135 --> 00:08:15,330
So, we'll call the callback and then the result will be the parameter to the callback.

105
00:08:15,330 --> 00:08:21,595
So, when we implement the usage of this function in our index.js file,

106
00:08:21,595 --> 00:08:23,930
we will be providing the callback there which will

107
00:08:23,930 --> 00:08:26,880
receive the result as the incoming parameter.

108
00:08:26,880 --> 00:08:30,670
So, this is for the "insertDocument."

109
00:08:30,670 --> 00:08:33,010
Now, for the "findDocument,

110
00:08:33,010 --> 00:08:36,750
" what I am going to do is to say,

111
00:08:37,010 --> 00:08:41,640
"coll.find" and I'm going to find all the documents.

112
00:08:41,640 --> 00:08:44,810
So, that's why I will give an empty JavaScript object in here,

113
00:08:44,810 --> 00:08:51,040
which will match with all the documents in the collection and then I'll say,

114
00:08:51,040 --> 00:09:01,820
"toArray" and this will take as a parameter a callback function,

115
00:09:02,220 --> 00:09:06,505
and inside this callback function, of course,

116
00:09:06,505 --> 00:09:11,710
I will assert that this is not null,

117
00:09:11,710 --> 00:09:20,590
and then we will say, "callback docs."

118
00:09:20,590 --> 00:09:27,440
We'll simply pass back the retrieved documents back to the calling function.

119
00:09:28,200 --> 00:09:37,750
Now, for the "removeDocument," I'm going to say, "collection deleteOne."

120
00:09:37,750 --> 00:09:40,270
So, I'll try to find the first document that

121
00:09:40,270 --> 00:09:45,130
matches what we have specified and then delete it.

122
00:09:45,130 --> 00:09:47,070
And then this will take, again,

123
00:09:47,070 --> 00:09:50,380
a callback function as a second parameter,

124
00:09:50,380 --> 00:09:52,685
and inside this callback function,

125
00:09:52,685 --> 00:09:58,160
the first thing that I check is to make sure that the error is not null.

126
00:09:58,160 --> 00:10:01,820
And then after that, we will say,

127
00:10:02,130 --> 00:10:20,890
"console log removed the document comma document."

128
00:10:20,890 --> 00:10:24,840
We used the comma here because this is a JavaScript object so,

129
00:10:24,840 --> 00:10:26,890
if you specify console log like this,

130
00:10:26,890 --> 00:10:30,360
the document will be printed out and then we'll pass

131
00:10:30,360 --> 00:10:36,705
that result back through the callback function.

132
00:10:36,705 --> 00:10:47,550
And then finally, for the update we'll coll.updateOne.

133
00:10:47,670 --> 00:10:52,255
This is a method the MongoDB driver supports.

134
00:10:52,255 --> 00:10:56,560
So, I'll say, "update.One document,

135
00:10:56,560 --> 00:11:01,480
" and the second parameter is where we

136
00:11:01,480 --> 00:11:06,960
will pass in the fields that need to be updated.

137
00:11:06,960 --> 00:11:08,915
And the way it is done,

138
00:11:08,915 --> 00:11:12,810
we'll say, "dollar set colon update."

139
00:11:12,810 --> 00:11:16,100
So, this will take the update information that

140
00:11:16,100 --> 00:11:19,120
I'm sending in and then pass it into the update.One.

141
00:11:19,120 --> 00:11:21,620
The first one is the document that needs to be updated.

142
00:11:21,620 --> 00:11:25,425
The second one is which fields of the document needs to be updated here.

143
00:11:25,425 --> 00:11:28,660
And the third parameter is null,

144
00:11:28,770 --> 00:11:36,305
and that last parameter is a callback function,

145
00:11:36,305 --> 00:11:45,660
which obviously will give us the result of the operation.

146
00:11:45,660 --> 00:11:51,125
So, first thing I'm going to check to make sure that the error is not null.

147
00:11:51,125 --> 00:11:59,535
Then, I'll do a "console log updated the document with."

148
00:11:59,535 --> 00:12:03,195
Now again, the console logs are purely for us to

149
00:12:03,195 --> 00:12:07,920
ensure that the code is doing what it is doing.

150
00:12:07,920 --> 00:12:09,690
It doesn't help in any way.

151
00:12:09,690 --> 00:12:11,995
This is just for our own information.

152
00:12:11,995 --> 00:12:15,070
In a production server,

153
00:12:15,070 --> 00:12:18,570
you would not be having this console logs, maybe.

154
00:12:18,570 --> 00:12:24,220
You can disable them. So, that's it.

155
00:12:24,220 --> 00:12:32,370
So four methods being supported in this particular file-based node module insert,

156
00:12:32,370 --> 00:12:35,040
find, remove and update.

157
00:12:35,040 --> 00:12:38,490
So, now that we have implemented this file-based node module,

158
00:12:38,490 --> 00:12:41,185
let's go to the index.js file,

159
00:12:41,185 --> 00:12:45,985
and then to make use of that file-based node module,

160
00:12:45,985 --> 00:12:48,010
I need to require this here.

161
00:12:48,010 --> 00:12:53,969
So, I'll say, "const dboper require."

162
00:12:53,969 --> 00:13:00,155
Since this is a file-based node module you get the full path to the node module,

163
00:13:00,155 --> 00:13:03,865
and which in this case happens to be./operations

164
00:13:03,865 --> 00:13:09,760
because it is in the same folder as my index.js file.

165
00:13:09,760 --> 00:13:12,400
Now, once we have done this,

166
00:13:12,400 --> 00:13:19,570
then right here, this function,

167
00:13:19,570 --> 00:13:24,355
this code that we were doing to access the database now insert,

168
00:13:24,355 --> 00:13:32,430
we will be using the dboper that we have just implemented to access that database.

169
00:13:32,430 --> 00:13:36,280
So, we'll say, "dboper insert document,

170
00:13:36,280 --> 00:13:40,345
" and this "insert document" takes the db as the first parameter.

171
00:13:40,345 --> 00:13:45,015
The db here, is this db that came in when we call MongoClient connect.

172
00:13:45,015 --> 00:13:48,010
So that db will be passed in, so that way,

173
00:13:48,010 --> 00:13:53,805
my operations node module knows where to access the database.

174
00:13:53,805 --> 00:13:59,325
Then the second parameter is the,

175
00:13:59,325 --> 00:14:03,950
so if you look at the "insert document" you will see

176
00:14:03,950 --> 00:14:08,055
that the second parameter is the document to be inserted.

177
00:14:08,055 --> 00:14:10,850
So, I will say, "name. "

178
00:14:11,640 --> 00:14:21,680
I'm just going to construct a JSON object or JavaScript object here,

179
00:14:21,680 --> 00:14:27,315
which will automatically be mapped into adjacent object when it is being inserted.

180
00:14:27,315 --> 00:14:30,135
And the third parameter, as you see,

181
00:14:30,135 --> 00:14:32,790
"db document collection and callback."

182
00:14:32,790 --> 00:14:36,690
So the third parameter is the collection and the collection is

183
00:14:36,690 --> 00:14:42,855
the dishes collection and the final one is the callback.

184
00:14:42,855 --> 00:14:48,605
The call back again as you recall receives the result.

185
00:14:48,605 --> 00:14:52,710
If you look back at the baby implemented the insert document,

186
00:14:52,710 --> 00:14:55,245
the callback received the result as the parameter.

187
00:14:55,245 --> 00:15:00,305
So inside this callback we will handle that result value here.

188
00:15:00,305 --> 00:15:07,000
So, when the result value comes in we'll do a console.log.

189
00:15:07,390 --> 00:15:12,750
We'll say insert document

190
00:15:13,240 --> 00:15:20,565
backslash N and we'll say result OPS.

191
00:15:20,565 --> 00:15:24,760
The OPS tells you the number of insert operations that were carried out.

192
00:15:24,760 --> 00:15:28,680
So this is another object that is going to be on the result

193
00:15:28,680 --> 00:15:31,790
JavaScript object that is passed back in as

194
00:15:31,790 --> 00:15:34,650
the parameter and so I'm just going to print out that value.

195
00:15:34,650 --> 00:15:38,260
So that'll give us some information about what has happened.

196
00:15:38,260 --> 00:15:40,610
Now once this is completed,

197
00:15:40,610 --> 00:15:45,880
inside this callback function I'm going to call the next database operation.

198
00:15:45,880 --> 00:15:50,730
So I'll say dboper and then find the documents.

199
00:15:50,730 --> 00:16:00,260
Then I'll say dbdishes and the third parameter is docs,

200
00:16:00,260 --> 00:16:03,090
which is the callback function.

201
00:16:03,090 --> 00:16:06,100
When I receive the docs I'm going to do a

202
00:16:06,100 --> 00:16:15,170
console.log saying found documents,

203
00:16:16,170 --> 00:16:22,140
and we'll simply lock the documents to the screen.

204
00:16:22,140 --> 00:16:25,075
So, this will print out the found documents.

205
00:16:25,075 --> 00:16:28,470
Notice that this call is inside

206
00:16:28,470 --> 00:16:37,480
the callback function that is applied for the earlier function call,

207
00:16:37,480 --> 00:16:41,225
so that is something that I want you to notice specifically.

208
00:16:41,225 --> 00:16:46,290
Now, again inside this function call they need to do it this way because until

209
00:16:46,290 --> 00:16:52,185
this callback is called we cannot do the next operation.

210
00:16:52,185 --> 00:16:58,855
So, in that next operation I'm going to update the document that I have just inserted.

211
00:16:58,855 --> 00:17:03,990
So I will say update the document and I'll say Update Document DB,

212
00:17:03,990 --> 00:17:07,880
and then the next parameter is the document

213
00:17:07,880 --> 00:17:11,800
and I don't need to specify the entire document I can only specify one field and

214
00:17:11,800 --> 00:17:20,740
then that'll find the document that matches this particular field.

215
00:17:21,960 --> 00:17:27,690
What I'm going to do is the next parameter is the update that needs to be supplied.

216
00:17:27,690 --> 00:17:30,770
So, the update is which field I want to update,

217
00:17:30,770 --> 00:17:35,350
so I'm going to update

218
00:17:35,350 --> 00:17:42,050
the description field by saying Updated Test,

219
00:17:43,030 --> 00:17:48,475
and then the fourth one is the collection which is Dishes,

220
00:17:48,475 --> 00:17:53,820
and the final one is the callback function which obtains

221
00:17:53,820 --> 00:18:01,110
the docs as the return value or

222
00:18:01,110 --> 00:18:08,705
rather result of the update operation that we just carried out.

223
00:18:08,705 --> 00:18:11,890
Then inside this callback function,

224
00:18:11,890 --> 00:18:20,845
I'm going to again do a console.log saying

225
00:18:20,845 --> 00:18:31,050
updated document backslash N. The updated document will be passed

226
00:18:31,050 --> 00:18:38,125
back in result.result on

227
00:18:38,125 --> 00:18:43,115
this property of the result object that is passed tracking.

228
00:18:43,115 --> 00:18:49,044
Again notice how the calls are getting nested

229
00:18:49,044 --> 00:18:54,585
inside the callback functions here.

230
00:18:54,585 --> 00:18:57,880
I want you to notice this structure of the code

231
00:18:57,880 --> 00:19:03,130
specifically because that is what I'm going to come back to in the next exercise.

232
00:19:03,130 --> 00:19:05,760
Now after I do that,

233
00:19:05,760 --> 00:19:07,529
I'm going to find the documents,

234
00:19:07,529 --> 00:19:12,930
so let me just copy that and then I'm going to use the same code here.

235
00:19:13,840 --> 00:19:19,530
So I will say, find documents.

236
00:19:20,000 --> 00:19:27,695
Inside here I am again going to say, found updated document.

237
00:19:27,695 --> 00:19:30,375
Then finally when I'm done with this,

238
00:19:30,375 --> 00:19:35,855
I will simply call db drop collection.

239
00:19:35,855 --> 00:19:38,760
So I'm going to delete this dishes collection so that I

240
00:19:38,760 --> 00:19:41,630
will clean up my database so that I don't have

241
00:19:41,630 --> 00:19:44,350
anything more because for the next exercise I want to clean up

242
00:19:44,350 --> 00:19:47,230
the database and then start with a cleaner database.

243
00:19:47,230 --> 00:19:48,835
So, I'm going to clean out

244
00:19:48,835 --> 00:19:57,770
the dishes collection and then this would result in a callback.

245
00:19:59,110 --> 00:20:07,410
So inside here I'm going to do a console.log saying,

246
00:20:07,410 --> 00:20:17,000
dropped collection, and then I'll just print out the result that came in.

247
00:20:17,820 --> 00:20:25,250
Then finally, close the database.

248
00:20:25,650 --> 00:20:28,895
Note that structure of the code,

249
00:20:28,895 --> 00:20:34,580
DB insert document and inside the callback function I'm going to call

250
00:20:34,580 --> 00:20:37,690
the next function and inside

251
00:20:37,690 --> 00:20:39,660
the callback off that I'm going to call

252
00:20:39,660 --> 00:20:42,270
the next function and inside the callback next function and so on.

253
00:20:42,270 --> 00:20:46,990
So, you see a nested set of callbacks here and

254
00:20:46,990 --> 00:20:52,665
a tree structured nested set of call backs here.

255
00:20:52,665 --> 00:20:56,505
That is something that I want you to pay attention to.

256
00:20:56,505 --> 00:21:02,680
Let's save the changes and go and look at this version of our application.

257
00:21:02,680 --> 00:21:04,725
Getting back to the terminal,

258
00:21:04,725 --> 00:21:08,045
let me execute the application.

259
00:21:08,045 --> 00:21:13,035
So let me type NPM start and we'll see that result.

260
00:21:13,035 --> 00:21:19,060
Now, from this result you can see that the document is inserted

261
00:21:19,060 --> 00:21:25,330
into the collection and that is the document that has been found.

262
00:21:25,330 --> 00:21:26,990
In the second step,

263
00:21:26,990 --> 00:21:31,540
we are finding the document so when we call the DB find document,

264
00:21:31,540 --> 00:21:36,175
so this is the document that is retrieved from my collection.

265
00:21:36,175 --> 00:21:39,010
Then I ask it to update the document with

266
00:21:39,010 --> 00:21:42,915
this and then you'll notice that it says updated document and then

267
00:21:42,915 --> 00:21:46,850
here it prints the result or OPS and it says N is equal to

268
00:21:46,850 --> 00:21:51,775
one number modified as one and it was okay.

269
00:21:51,775 --> 00:21:57,010
Then it prints out the updated document,

270
00:21:57,010 --> 00:22:02,020
we find the updated document here and that is what is printed out here,

271
00:22:02,020 --> 00:22:05,770
and note in particular that the description has been updated.

272
00:22:05,770 --> 00:22:09,390
Then finally, it dropped the collection.

273
00:22:09,990 --> 00:22:16,690
So we see how this application runs and is able to make use of

274
00:22:16,690 --> 00:22:23,410
the node module that we implemented and then performed various database operations.

275
00:22:23,410 --> 00:22:26,560
With this we complete this exercise.

276
00:22:26,560 --> 00:22:31,520
In this exercise I have demonstrated to you how you would interact with

277
00:22:31,520 --> 00:22:37,085
your MongoDB server from your node application.

278
00:22:37,085 --> 00:22:43,455
We have implemented our own node module here and then used it

279
00:22:43,455 --> 00:22:46,810
within our node application by encapsulating

280
00:22:46,810 --> 00:22:50,965
certain of the database operations into that node module.

281
00:22:50,965 --> 00:22:54,020
This is a good time for you to do a Git commit with

282
00:22:54,020 --> 00:22:58,860
the message node MongoDB example part two.