1
00:00:03,920 --> 00:00:10,025
Now that we have learnt about the node MongoDB driver, the node module,

2
00:00:10,025 --> 00:00:14,205
which enables our node application to be able to interact

3
00:00:14,205 --> 00:00:18,475
with a MongoDB server, in this exercise,

4
00:00:18,475 --> 00:00:23,430
we will use the node module and use the various methods that are

5
00:00:23,430 --> 00:00:29,840
supported by the node module to provide introduction with our MongoDB server.

6
00:00:29,840 --> 00:00:33,030
To get started, go to

7
00:00:33,030 --> 00:00:38,135
the NodeJS folder that you have been using to store all your examples,

8
00:00:38,135 --> 00:00:45,310
and then create a new folder there named node-mongo.

9
00:00:45,320 --> 00:00:48,125
In this folder, we will develop

10
00:00:48,125 --> 00:00:52,715
the NodeJS application that interacts with our MongoDB server.

11
00:00:52,715 --> 00:00:57,140
Now, make sure that your MongoDB server is up and running,

12
00:00:57,140 --> 00:01:02,675
so in the previous exercise we have already seen how our MongoDB server can be started.

13
00:01:02,675 --> 00:01:07,370
So, keep the MongoDB server up and running and in

14
00:01:07,370 --> 00:01:12,060
other terminal window or terminal tab or command window,

15
00:01:12,060 --> 00:01:15,020
go to the node-mongo folder that you have just

16
00:01:15,020 --> 00:01:18,135
created and initialize a node application here.

17
00:01:18,135 --> 00:01:22,910
So, let me type npm init and we

18
00:01:22,910 --> 00:01:29,765
will name the application as Node-Mongo and version,

19
00:01:29,765 --> 00:01:36,895
description Node MongoDB Example,

20
00:01:36,895 --> 00:01:39,295
and the entry point,

21
00:01:39,295 --> 00:01:48,980
and the author, just like we have been filling in the previous exercises.

22
00:01:48,980 --> 00:01:53,255
Then once the package.json file is initialized,

23
00:01:53,255 --> 00:01:59,610
open this in the editor of your choice.

24
00:01:59,610 --> 00:02:02,635
Now going into the package.json file,

25
00:02:02,635 --> 00:02:09,370
let me introduce another script here just like we have been doing earlier, so,

26
00:02:09,370 --> 00:02:16,350
we'll say "start": "node index" and save the changes.

27
00:02:16,350 --> 00:02:18,010
Going back to the terminal,

28
00:02:18,010 --> 00:02:22,350
let's install the MongoDB node module.

29
00:02:22,350 --> 00:02:30,080
So, type npm install mongodb@3.0.10 --save.

30
00:02:30,080 --> 00:02:33,590
We will also install the assert node module, so,

31
00:02:33,590 --> 00:02:39,460
we'll say npm install assert@1.4.1 --save.

32
00:02:39,460 --> 00:02:42,710
The assert module enables us to use the assert to check

33
00:02:42,710 --> 00:02:46,720
for truth or false values within our application,

34
00:02:46,720 --> 00:02:49,535
and you will see me using it later.

35
00:02:49,535 --> 00:02:52,645
Now that we have installed these node modules,

36
00:02:52,645 --> 00:02:58,355
let's create our node application that interacts with the MongoDB server.

37
00:02:58,355 --> 00:03:00,455
Going back to the editor,

38
00:03:00,455 --> 00:03:01,915
in the project folder,

39
00:03:01,915 --> 00:03:05,465
create a file named index.js,

40
00:03:05,465 --> 00:03:07,684
and this is where we will create

41
00:03:07,684 --> 00:03:12,030
the node application that interacts with our MongoDB server.

42
00:03:12,030 --> 00:03:14,425
So, in this application,

43
00:03:14,425 --> 00:03:18,630
let's type const Mongoclient,

44
00:03:18,800 --> 00:03:21,555
and then they'll say,

45
00:03:21,555 --> 00:03:28,160
require( `mongodb`), the MongoDB node module that we have already installed,

46
00:03:28,160 --> 00:03:30,335
and then this would provide us with

47
00:03:30,335 --> 00:03:38,045
the MongoClient that enables us to connect to the MongoDB server.

48
00:03:38,045 --> 00:03:47,160
Also, require the assert module.

49
00:03:48,830 --> 00:03:53,870
Now, to start up a connection to the MongoDB server,

50
00:03:53,870 --> 00:04:03,590
let's create a constant name URL which will be the URL where the MongoDB server can be

51
00:04:03,590 --> 00:04:08,030
accessed and the baby access this is

52
00:04:08,030 --> 00:04:14,950
by saying mongodb://localhost: 27017/.

53
00:04:14,950 --> 00:04:19,360
Recall that this is the port number at which your MongoDB server is

54
00:04:19,360 --> 00:04:25,025
running 27017 const dbname=,

55
00:04:25,025 --> 00:04:27,205
and then they we'll say conFusion.

56
00:04:27,205 --> 00:04:28,820
Recall that we created

57
00:04:28,820 --> 00:04:34,060
the conFusion database in our MongoDB server in the previous exercise,

58
00:04:34,060 --> 00:04:39,310
so, we're going to access that the conFusion database that we have created earlier.

59
00:04:39,310 --> 00:04:46,240
Now, to access the server, we`ll say MongoClient.connect.

60
00:04:48,690 --> 00:04:54,790
So, the connect method allows us to connect to the MongoClient from our MongoDB server,

61
00:04:54,790 --> 00:04:59,230
and then we'll say this takes the URL as the first parameter,

62
00:04:59,230 --> 00:05:02,485
the URL of the MongoDB server as the first parameter.

63
00:05:02,485 --> 00:05:06,590
The second parameter is a callback function.

64
00:05:06,590 --> 00:05:14,275
So, the callback function has two parameters, the error value,

65
00:05:14,275 --> 00:05:17,915
and client, which we can then use

66
00:05:17,915 --> 00:05:22,405
to connect to our database and then perform various operations.

67
00:05:22,405 --> 00:05:27,040
So, inside this callback function,

68
00:05:27,040 --> 00:05:36,400
first thing that we will do is to check to make sure that the error is not null.

69
00:05:36,400 --> 00:05:39,740
So, the assert will check to see if error is equal to null,

70
00:05:39,740 --> 00:05:45,640
so the assert function allows us to perform various checks on values.

71
00:05:45,640 --> 00:05:48,010
So, we check to make sure that the error is not,

72
00:05:48,010 --> 00:05:49,785
if it is not null,

73
00:05:49,785 --> 00:05:55,660
then that means that there is an error and so we will show the error on the screen.

74
00:05:55,660 --> 00:05:59,650
So, that's the use of the assert function here.

75
00:05:59,650 --> 00:06:03,025
Now, if that doesn't happen,

76
00:06:03,025 --> 00:06:07,430
then that means that we have connected properly to the server,

77
00:06:07,430 --> 00:06:15,170
so we'll say `connected correctly to server`

78
00:06:15,170 --> 00:06:25,120
const db= client.db(dbname) to connect to the database,

79
00:06:25,120 --> 00:06:33,570
then we can say const collection db.,

80
00:06:33,570 --> 00:06:38,810
so the db will provide us with various methods that enable us to

81
00:06:38,810 --> 00:06:44,380
interact with the server so we'll say db.collection(` dishes`) So,

82
00:06:44,380 --> 00:06:49,765
we'll try to access the dishes collection within this database.

83
00:06:49,765 --> 00:06:55,175
Now, the next thing that we will try to do is

84
00:06:55,175 --> 00:07:04,414
collection.insertone so we will try to insert one object into the collection,

85
00:07:04,414 --> 00:07:08,360
and this object would be with

86
00:07:08,360 --> 00:07:28,510
the name and description, "test".

87
00:07:28,510 --> 00:07:32,520
So, this collection provides this method called insertOne,

88
00:07:32,520 --> 00:07:36,825
which allows us to insert one document into this collection,

89
00:07:36,825 --> 00:07:42,825
and the document is the first argument to this method here.

90
00:07:42,825 --> 00:07:49,780
The second argument to the method is a callback function,

91
00:07:49,780 --> 00:07:56,720
and the callback function takes two parameters, error and result.

92
00:07:58,260 --> 00:08:01,000
So, within this callback function,

93
00:08:01,000 --> 00:08:03,180
if the result is obtained,

94
00:08:03,180 --> 00:08:08,610
then we are able to access the collection and then perform further operations.

95
00:08:08,610 --> 00:08:16,645
So, here, the first thing that we will do is check to make sure that error is not null.

96
00:08:16,645 --> 00:08:19,450
So, we'll do an assertion there,

97
00:08:19,450 --> 00:08:33,470
and then here we can say, "After Insert".

98
00:08:37,770 --> 00:08:45,145
So, after we insert, then we see that we

99
00:08:45,145 --> 00:08:51,490
get the result value that is returned there, so this result.

100
00:08:51,490 --> 00:08:56,070
This result will also provide

101
00:08:56,070 --> 00:09:00,805
this OPS property which says how many operations have just been carried out successfully.

102
00:09:00,805 --> 00:09:02,715
So, that is what I am going to print out.

103
00:09:02,715 --> 00:09:05,140
So, it will say number inserted is one.

104
00:09:05,140 --> 00:09:12,030
So, which means that this particular document has been inserted into the database.

105
00:09:12,070 --> 00:09:15,440
So, that's what we will confirm here.

106
00:09:15,440 --> 00:09:19,810
Then, after that, we will perform another option,

107
00:09:19,810 --> 00:09:22,415
an operation on the collection, and say,

108
00:09:22,415 --> 00:09:30,200
collection find, and we'll try to search for all the records in the collection.

109
00:09:30,200 --> 00:09:37,420
So, here, we will supply an empty JSON string or corresponding level JavaScript object.

110
00:09:37,420 --> 00:09:40,895
So, it will search for everything that is there in the collection,

111
00:09:40,895 --> 00:09:43,750
and then, provide that to us.

112
00:09:43,750 --> 00:09:48,640
Then, this can be converted to an array of JSON objects.

113
00:09:48,640 --> 00:09:52,215
So, this is why we call the toArray,

114
00:09:52,215 --> 00:09:59,365
and this takes a callback function as the parameter.

115
00:09:59,365 --> 00:10:07,065
The first parameter of course is the error value, so again,

116
00:10:07,065 --> 00:10:16,365
we check to make sure that the error is not null.

117
00:10:16,365 --> 00:10:25,990
Then we'll say, console.log found,

118
00:10:25,990 --> 00:10:29,140
and then print out whatever that has been found,

119
00:10:29,140 --> 00:10:32,505
and we'll say console.log.

120
00:10:32,505 --> 00:10:35,080
So, the second parameter here, docs,

121
00:10:35,080 --> 00:10:38,370
will return all the documents from this collection

122
00:10:38,370 --> 00:10:42,380
that match whatever criteria that you specify here.

123
00:10:42,380 --> 00:10:43,970
Since this is empty,

124
00:10:43,970 --> 00:10:47,535
then that means that all the documents in the collection will be returned to us.

125
00:10:47,535 --> 00:10:49,500
You can specify a filter here,

126
00:10:49,500 --> 00:10:54,030
saying name is equal to a value,

127
00:10:54,030 --> 00:10:57,325
and then only those documents that match that value will be retrieved.

128
00:10:57,325 --> 00:11:04,040
So here, let me just print out the docs here,

129
00:11:04,040 --> 00:11:07,660
to indicate that this item has

130
00:11:07,660 --> 00:11:11,990
been inserted into the collection and then will be retrieved,

131
00:11:11,990 --> 00:11:15,950
and then we'll just print it out just to make sure that the document that we just

132
00:11:15,950 --> 00:11:20,590
inserted in the previous operation is indeed in the collection,

133
00:11:20,590 --> 00:11:22,420
and so we'll print that out here.

134
00:11:22,420 --> 00:11:28,060
Now after this, then we will use

135
00:11:28,060 --> 00:11:35,880
the dropCollection method here.

136
00:11:35,880 --> 00:11:40,560
So, the dropCollection will drop the specified collection here.

137
00:11:40,560 --> 00:11:42,410
So, we'll say dropCollection dishes,

138
00:11:42,410 --> 00:11:45,820
so I'm just going to remove the dishes collection from my database.

139
00:11:45,820 --> 00:11:48,180
So, I am just going to clean up the database and then leave

140
00:11:48,180 --> 00:11:51,090
it with an empty database so that we can go onto

141
00:11:51,090 --> 00:11:53,610
the next exercise where we can again

142
00:11:53,610 --> 00:11:57,115
perform similar kind of operations to check this out.

143
00:11:57,115 --> 00:12:00,455
This one takes, as a second parameter,

144
00:12:00,455 --> 00:12:06,815
a callback function which returns either an error or a result.

145
00:12:06,815 --> 00:12:16,040
Inside this, we will check and make sure that the assert is not null,

146
00:12:16,440 --> 00:12:20,780
and then we will say, client.close.

147
00:12:20,780 --> 00:12:25,425
So we will close the connection to the database on the spot.

148
00:12:25,425 --> 00:12:34,465
So with this, this simple set of methods enable us to interact with our server,

149
00:12:34,465 --> 00:12:41,150
and also it demonstrates to us how we can communicate with the server using

150
00:12:41,150 --> 00:12:48,185
the methods that are available through this MongoDB node module or the MongoDB driver.

151
00:12:48,185 --> 00:12:51,900
So, here, you see that first we perform an insert operation.

152
00:12:51,900 --> 00:12:54,875
Then after the insert operation, note that,

153
00:12:54,875 --> 00:12:59,715
we are doing the second operation inside the callback function.

154
00:12:59,715 --> 00:13:07,490
So, this ensures that this operation has been completed before you do the next one.

155
00:13:07,490 --> 00:13:15,680
So, this is a second call which is enclosed inside the first callback function,

156
00:13:15,680 --> 00:13:19,470
and then correspondingly, inside of that.

157
00:13:19,470 --> 00:13:24,370
We are again calling the methods here and inside there.

158
00:13:24,370 --> 00:13:28,080
So, you see that there is a nesting of these calls,

159
00:13:28,080 --> 00:13:29,435
one inside the other.

160
00:13:29,435 --> 00:13:32,200
Note the structure because I'm going to come back to

161
00:13:32,200 --> 00:13:35,755
explain something about this in one of the later exercises.

162
00:13:35,755 --> 00:13:40,235
So, note that, nesting of these calls one inside the other.

163
00:13:40,235 --> 00:13:43,020
Let's save the changes,

164
00:13:43,210 --> 00:13:49,330
and then see what this application does when we execute it.

165
00:13:49,330 --> 00:13:51,685
Going back to the terminal, again,

166
00:13:51,685 --> 00:13:55,170
make sure that your MongoDB server is up and running, and so,

167
00:13:55,170 --> 00:14:00,560
when it is running it is going to print out a lot of log messages onto the console.

168
00:14:00,560 --> 00:14:04,230
We can just ignore those log messages for now.

169
00:14:04,230 --> 00:14:06,710
Let's run the node application.

170
00:14:06,710 --> 00:14:09,330
So, we'll say, npm start,

171
00:14:09,330 --> 00:14:12,095
and when the node application runs,

172
00:14:12,095 --> 00:14:14,875
you notice what does node application is doing.

173
00:14:14,875 --> 00:14:19,705
So it says, connected correctly to the server, After Insert,

174
00:14:19,705 --> 00:14:22,260
then it shows that

175
00:14:22,260 --> 00:14:28,375
this particular document has been inserted into our MongoDB and the node in particular,

176
00:14:28,375 --> 00:14:29,540
the ID given here.

177
00:14:29,540 --> 00:14:32,570
So, notice that this doesn't say this is a object ID.

178
00:14:32,570 --> 00:14:35,995
This ID is automatically inserted by

179
00:14:35,995 --> 00:14:39,730
the node Mongo driver into their document when

180
00:14:39,730 --> 00:14:43,745
you try to insert it into the MongoDB server here.

181
00:14:43,745 --> 00:14:48,950
This format is exactly the same as the object ID that we have seen.

182
00:14:48,950 --> 00:14:53,910
Thereafter, we performed a find operation.

183
00:14:53,910 --> 00:14:55,560
So, this is found,

184
00:14:55,560 --> 00:14:58,710
and then it prints out this item here.

185
00:14:58,710 --> 00:15:01,550
Note that from the previous exercise,

186
00:15:01,550 --> 00:15:08,470
we had left over in earlier document that we had inserted in the previous exercise.

187
00:15:08,470 --> 00:15:11,005
So, that is still seen in the collection here,

188
00:15:11,005 --> 00:15:13,500
and the new document that we just inserted here.

189
00:15:13,500 --> 00:15:17,179
This document is seen right below that here,

190
00:15:17,179 --> 00:15:20,510
but note that these two have two different IDs.

191
00:15:20,510 --> 00:15:25,030
This ID matches the ID of the document that we just inserted.

192
00:15:25,030 --> 00:15:28,455
So, this is leftover from the previous exercise.

193
00:15:28,455 --> 00:15:32,765
After this, of course we drop the collection so

194
00:15:32,765 --> 00:15:38,060
this whole set of documents and the dishes collection will be removed from our database.

195
00:15:38,060 --> 00:15:41,060
So, now our database will be clean and empty.

196
00:15:41,060 --> 00:15:44,930
So, that demonstrates to you how we can use

197
00:15:44,930 --> 00:15:52,900
the MongoDB driver to communicate from our node application with the server.

198
00:15:52,900 --> 00:15:55,330
Now that we have our application,

199
00:15:55,330 --> 00:16:01,000
let me introduce a.gitignore

200
00:16:01,000 --> 00:16:06,720
here and then put node modules into the.gitignore,

201
00:16:06,720 --> 00:16:10,540
and you quickly understand why I'm doing this.

202
00:16:10,540 --> 00:16:16,595
Then, we will go back and initialize the Git repository and then do a git commit.

203
00:16:16,595 --> 00:16:18,590
Going back to the terminal,

204
00:16:18,590 --> 00:16:21,505
let me initialize the Git repository,

205
00:16:21,505 --> 00:16:31,460
and then, let's check the Git status and you see that there are some uncommitted files.

206
00:16:31,460 --> 00:16:37,204
So let me check them in and then do a git commit with the message,

207
00:16:37,204 --> 00:16:46,450
Node MongoDB Example one.

208
00:16:46,450 --> 00:16:49,305
This is the part one of the exercise,

209
00:16:49,305 --> 00:16:54,020
so we'll just check the items into our git repository.

210
00:16:54,020 --> 00:16:56,900
This completes this exercise.

211
00:16:56,900 --> 00:17:01,760
In this exercise, we have seen how we install the node module,

212
00:17:01,760 --> 00:17:04,365
the MongoDB driver, then,

213
00:17:04,365 --> 00:17:07,835
we were able to communicate with our MongoDB server from within

214
00:17:07,835 --> 00:17:12,740
our node application using the methods that are provided by the MongoDB driver.

215
00:17:12,740 --> 00:17:17,840
We will continue with this exercise in the next part of

216
00:17:17,840 --> 00:17:24,380
the exercise where we will explore a few more methods of the MongoDB driver.