1
00:00:02,110 --> 00:00:05,110
Now just as with MySQL and NodeJS

2
00:00:05,110 --> 00:00:08,590
in order to connect to the MongoDB database

3
00:00:08,590 --> 00:00:10,350
from inside our NodeJS code,

4
00:00:10,350 --> 00:00:12,670
we wanna use a third-party package,

5
00:00:12,670 --> 00:00:15,640
so that we don't need to manage the technical details

6
00:00:15,640 --> 00:00:19,820
of establishing and holding such a connection on our own.

7
00:00:19,820 --> 00:00:22,880
And for this you can search for NodeJS MongoDB,

8
00:00:22,880 --> 00:00:26,650
and you'll find your official MongoDB Node Driver.

9
00:00:26,650 --> 00:00:29,520
So a package created by the MongoDB team,

10
00:00:29,520 --> 00:00:32,130
which is meant for exactly this use case

11
00:00:32,130 --> 00:00:35,943
that you wanna connect to MongoDB from inside NodeJS.

12
00:00:37,740 --> 00:00:39,990
Now, if you'd click on Quick Start there,

13
00:00:39,990 --> 00:00:42,400
you'll learn how you can set up a project

14
00:00:42,400 --> 00:00:46,040
and how you can install this MongoDB driver.

15
00:00:46,040 --> 00:00:47,573
It's this simple command.

16
00:00:48,440 --> 00:00:50,120
So we can copy this command.

17
00:00:50,120 --> 00:00:53,340
And then quit does running nodemon server here

18
00:00:53,340 --> 00:00:54,240
for the moment,

19
00:00:54,240 --> 00:00:57,210
and then run npm install mongodb

20
00:00:57,210 --> 00:00:59,690
to install this third-party package,

21
00:00:59,690 --> 00:01:02,690
which therefore was also added as a dependency

22
00:01:02,690 --> 00:01:04,653
to the package.json file.

23
00:01:05,598 --> 00:01:09,083
And then we can restart our node server.

24
00:01:10,214 --> 00:01:13,260
Now this alone, of course doesn't connect to the database,

25
00:01:13,260 --> 00:01:16,990
it just gives us a package that helps with the connection.

26
00:01:16,990 --> 00:01:18,170
And to manage the connection,

27
00:01:18,170 --> 00:01:21,950
I'll create a new folder in this project,

28
00:01:21,950 --> 00:01:25,623
the data folder with a database.js file.

29
00:01:26,460 --> 00:01:28,740
And in there, I wanna manage that connection

30
00:01:28,740 --> 00:01:30,590
so that in the other files

31
00:01:30,590 --> 00:01:32,790
where I wanna use that connection,

32
00:01:32,790 --> 00:01:35,330
I can just reach out to the single file.

33
00:01:35,330 --> 00:01:38,070
And I don't have to establish a new connection

34
00:01:38,070 --> 00:01:41,683
in every file where I wanna work with the database.

35
00:01:43,394 --> 00:01:45,920
Now, there are various ways of implementing

36
00:01:45,920 --> 00:01:48,900
such a connection and of managing such a connection,

37
00:01:48,900 --> 00:01:52,210
but here's a very common pattern which you can use

38
00:01:52,210 --> 00:01:55,280
in such a global database management file like this

39
00:01:55,280 --> 00:01:57,410
when working with MongoDB.

40
00:01:57,410 --> 00:02:01,610
We first start by importing this MongoDB package,

41
00:02:01,610 --> 00:02:03,600
which we just installed.

42
00:02:03,600 --> 00:02:06,270
By requiring mongodb like this

43
00:02:06,270 --> 00:02:08,652
in this database.js file here.

44
00:02:10,190 --> 00:02:15,190
Then as a next step, we can get the Mongo client

45
00:02:15,250 --> 00:02:18,563
by accessing mongodb.MongoClient.

46
00:02:19,667 --> 00:02:23,635
That's the actual thing that we need from the package

47
00:02:23,635 --> 00:02:25,802
to establish a connection.

48
00:02:27,295 --> 00:02:30,420
Now, this Mongo client can be used

49
00:02:30,420 --> 00:02:33,530
to call the connect method to then

50
00:02:33,530 --> 00:02:35,930
do exactly what the name suggests,

51
00:02:35,930 --> 00:02:39,230
to create a connection to a MongoDB server.

52
00:02:39,230 --> 00:02:40,810
So in this case to the server,

53
00:02:40,810 --> 00:02:43,093
which is running locally on our machine.

54
00:02:44,850 --> 00:02:48,260
So here we can call connect.

55
00:02:48,260 --> 00:02:50,100
And connect needs some information

56
00:02:50,100 --> 00:02:52,773
on how to establish this connection.

57
00:02:53,710 --> 00:02:56,480
Therefore we can pass and parameter values.

58
00:02:56,480 --> 00:02:59,560
And the first parameter value which we can pass

59
00:02:59,560 --> 00:03:03,493
is a URL that points to the MongoDB server.

60
00:03:04,800 --> 00:03:07,960
Now the format of this URL might look a bit weird,

61
00:03:07,960 --> 00:03:09,650
but it is a format understood

62
00:03:09,650 --> 00:03:12,120
by this third-party package here.

63
00:03:12,120 --> 00:03:17,080
It looks like this, you type mongodb://

64
00:03:17,080 --> 00:03:21,280
and then localhost:27017,

65
00:03:21,280 --> 00:03:23,480
since that is the address that will be used

66
00:03:23,480 --> 00:03:27,113
by that MongoDB server on our local system.

67
00:03:28,850 --> 00:03:31,140
Now, you know this from the official docs.

68
00:03:31,140 --> 00:03:32,870
For example, if you look at them

69
00:03:32,870 --> 00:03:35,363
or of course by simply believing me here.

70
00:03:36,500 --> 00:03:38,190
This year might look a bit strange,

71
00:03:38,190 --> 00:03:40,760
but it's simply telling the MongoDB driver

72
00:03:40,760 --> 00:03:43,520
that it should connect to this server

73
00:03:43,520 --> 00:03:46,190
with a special protocol, so to say.

74
00:03:46,190 --> 00:03:48,130
Which are some low level details

75
00:03:48,130 --> 00:03:50,030
we don't have to worry about.

76
00:03:50,030 --> 00:03:52,300
In the end, this will be the URL pointing

77
00:03:52,300 --> 00:03:55,290
to our locally running MongoDB server.

78
00:03:55,290 --> 00:03:57,320
That's the port number that's being used

79
00:03:57,320 --> 00:03:59,900
by that server out of the box by default.

80
00:03:59,900 --> 00:04:03,353
And therefore, this should establish such a connection.

81
00:04:04,640 --> 00:04:09,190
You'll also find this here in the official documentation.

82
00:04:09,190 --> 00:04:12,760
If you have a look at their example,

83
00:04:12,760 --> 00:04:16,040
they are using a slightly different URL here

84
00:04:16,040 --> 00:04:18,490
because they are connecting to a cluster.

85
00:04:18,490 --> 00:04:21,079
But if you have a look at connect to localhost,

86
00:04:21,079 --> 00:04:23,760
you see exactly this address I showed you

87
00:04:23,760 --> 00:04:25,223
a couple of seconds ago.

88
00:04:26,120 --> 00:04:30,030
So that's how we can connect to our MongoDB server here.

89
00:04:30,030 --> 00:04:32,800
We'll come back to connecting to such a cluster here

90
00:04:32,800 --> 00:04:35,940
later in the course, once we talk about deployment

91
00:04:35,940 --> 00:04:39,920
and about running a database for a real website,

92
00:04:39,920 --> 00:04:42,530
that's being exposed to users.

93
00:04:42,530 --> 00:04:45,130
But for development, this local host solution

94
00:04:45,130 --> 00:04:46,533
is absolutely fine.

95
00:04:47,870 --> 00:04:50,720
Now this would establish such a connection,

96
00:04:50,720 --> 00:04:53,590
and it actually establishes such a connection

97
00:04:53,590 --> 00:04:56,490
by returning a so-called promise.

98
00:04:56,490 --> 00:04:59,834
And we learned about promises in this more advanced

99
00:04:59,834 --> 00:05:03,370
JavaScript course section a couple of hours ago.

100
00:05:03,370 --> 00:05:06,070
And we also already used promises

101
00:05:06,070 --> 00:05:08,510
when we worked with MySQL.

102
00:05:08,510 --> 00:05:11,280
So I'm not going to explain them in depth here,

103
00:05:11,280 --> 00:05:14,430
instead of check out those earlier course sections

104
00:05:14,430 --> 00:05:17,240
to understand what promises are.

105
00:05:17,240 --> 00:05:18,780
In the end, promises allow us

106
00:05:18,780 --> 00:05:21,130
to work with asynchronous code.

107
00:05:21,130 --> 00:05:23,580
So code that can take a little bit longer

108
00:05:23,580 --> 00:05:25,410
to execute, so to say.

109
00:05:25,410 --> 00:05:27,450
And here establishing such a connection

110
00:05:27,450 --> 00:05:29,010
can take a bit longer,

111
00:05:29,010 --> 00:05:32,383
therefore this is actually returning such a promise.

112
00:05:33,550 --> 00:05:35,570
And therefore what I'll actually do here

113
00:05:35,570 --> 00:05:37,350
is I'll create a new function.

114
00:05:37,350 --> 00:05:39,810
Connect could be the name.

115
00:05:39,810 --> 00:05:42,890
And I'll turn this into an async function

116
00:05:42,890 --> 00:05:44,553
with the async keyword,

117
00:05:45,650 --> 00:05:48,620
and then move this line of code into this function,

118
00:05:48,620 --> 00:05:50,810
and then use the await keyword

119
00:05:50,810 --> 00:05:54,060
to wait for this operation to finish.

120
00:05:54,060 --> 00:05:56,670
And then when using does await keyword,

121
00:05:56,670 --> 00:06:00,200
this will eventually give us the database client,

122
00:06:00,200 --> 00:06:01,340
which we now have.

123
00:06:01,340 --> 00:06:04,683
So which now gives us access to the connected database.

124
00:06:06,010 --> 00:06:08,460
So now whenever we call the connect function

125
00:06:08,460 --> 00:06:11,570
from anywhere in our code, which we're not doing yet,

126
00:06:11,570 --> 00:06:13,220
but which we will do soon,

127
00:06:13,220 --> 00:06:16,533
will establish such a connection and get such a client.

128
00:06:19,120 --> 00:06:21,040
Now, as a side note, this connection,

129
00:06:21,040 --> 00:06:22,350
which is established here,

130
00:06:22,350 --> 00:06:25,750
will actually automatically be a connection pool,

131
00:06:25,750 --> 00:06:28,860
which is managed by the MongoDB package.

132
00:06:28,860 --> 00:06:31,650
So multiple connections will be established.

133
00:06:31,650 --> 00:06:34,280
And whenever you then need to run a query,

134
00:06:34,280 --> 00:06:36,770
you will automatically get an open connection,

135
00:06:36,770 --> 00:06:37,870
which you can use.

136
00:06:37,870 --> 00:06:40,810
And the MongoDB package will manage

137
00:06:40,810 --> 00:06:44,210
all those connections for us, which is very convenient.

138
00:06:44,210 --> 00:06:46,203
So we don't have to worry about that.

139
00:06:47,920 --> 00:06:49,330
Hence, we get this client,

140
00:06:49,330 --> 00:06:51,340
which would allow us to interact

141
00:06:51,340 --> 00:06:53,330
with the connected database,

142
00:06:53,330 --> 00:06:56,870
but I don't wanna run my queries and commands

143
00:06:56,870 --> 00:06:58,270
from inside this file.

144
00:06:58,270 --> 00:07:02,160
I wanna run those commands from inside another file.

145
00:07:02,160 --> 00:07:03,790
And therefore what I'll do here

146
00:07:03,790 --> 00:07:05,500
is I'll add a new variable.

147
00:07:05,500 --> 00:07:07,830
Database, could be the name.

148
00:07:07,830 --> 00:07:12,830
And actually I will set database equal to client.db here.

149
00:07:15,850 --> 00:07:18,700
And db is a method that we can execute

150
00:07:18,700 --> 00:07:22,140
to connect to a specific database on the server

151
00:07:22,990 --> 00:07:25,240
because here we only establish a connection

152
00:07:25,240 --> 00:07:27,080
to the server as a whole.

153
00:07:27,080 --> 00:07:30,440
Now we can call the db method to actually talk

154
00:07:30,440 --> 00:07:33,880
to the blog database on that server.

155
00:07:33,880 --> 00:07:36,330
And that is the database we worked with

156
00:07:36,330 --> 00:07:38,873
a couple of minutes ago in the MongoDB shell.

157
00:07:40,090 --> 00:07:44,408
And the result of calling db is an access to exactly

158
00:07:44,408 --> 00:07:47,750
that database on the MongoDB server

159
00:07:47,750 --> 00:07:50,790
and that stands for what I store in this database variable,

160
00:07:50,790 --> 00:07:52,660
which is deliberately created

161
00:07:52,660 --> 00:07:54,510
outside of this connect function

162
00:07:54,510 --> 00:07:57,000
so that it can be used in this whole file,

163
00:07:57,000 --> 00:07:59,563
and not just in this connect function.

164
00:08:00,760 --> 00:08:03,580
Because my goal is to make this variable

165
00:08:03,580 --> 00:08:06,143
available outside of this file as well now.

166
00:08:07,800 --> 00:08:10,977
Simply by adding another function,

167
00:08:10,977 --> 00:08:13,810
getDb could be the name

168
00:08:13,810 --> 00:08:16,733
where I simply return database,

169
00:08:17,780 --> 00:08:18,683
like this.

170
00:08:20,157 --> 00:08:24,660
And we could even check if databases may be not set

171
00:08:24,660 --> 00:08:26,720
if it's falsey,

172
00:08:26,720 --> 00:08:30,030
and if that's the case, we could throw an error

173
00:08:30,030 --> 00:08:35,030
where we say "Database connection not established!"

174
00:08:35,789 --> 00:08:38,270
Just to ensure that we're not trying to work

175
00:08:38,270 --> 00:08:41,750
with that database before we actually have it.

176
00:08:41,750 --> 00:08:43,669
So that connect must've been called

177
00:08:43,669 --> 00:08:46,470
before we try to use the database.

178
00:08:46,470 --> 00:08:49,270
That's a little security check for ourselves,

179
00:08:49,270 --> 00:08:50,830
which we can add here.

180
00:08:50,830 --> 00:08:54,493
It's not mandatory, but also not a bad idea.

181
00:08:56,340 --> 00:08:58,570
And now as a last step here,

182
00:08:58,570 --> 00:09:02,830
I will add the module.exports statement

183
00:09:02,830 --> 00:09:05,830
and export an object here.

184
00:09:05,830 --> 00:09:07,590
And that will be an object

185
00:09:07,590 --> 00:09:12,590
where I expose both these functions to the outside world.

186
00:09:14,110 --> 00:09:18,570
So I'll add connectToDatabase as a key here

187
00:09:18,570 --> 00:09:22,110
and point at the connect function like this.

188
00:09:22,110 --> 00:09:25,670
And then as a second property in this exported object,

189
00:09:25,670 --> 00:09:28,673
I'll add getDb as a key and point at getDb.

190
00:09:30,691 --> 00:09:32,740
So the things on the right side of the colons

191
00:09:32,740 --> 00:09:36,290
are these function names we have here in this file.

192
00:09:36,290 --> 00:09:40,010
And by just pointing at them and not executing them,

193
00:09:40,010 --> 00:09:42,100
so by not adding parentheses,

194
00:09:42,100 --> 00:09:44,920
we make sure that they are not executed yet,

195
00:09:44,920 --> 00:09:48,330
but that instead they can be executed in our files,

196
00:09:48,330 --> 00:09:50,520
which import this file,

197
00:09:50,520 --> 00:09:53,310
which is exactly what I wanna achieve.

198
00:09:53,310 --> 00:09:56,160
This file should just be a utility file.

199
00:09:56,160 --> 00:09:58,470
The functions should actually be executed

200
00:09:58,470 --> 00:10:00,513
from somewhere else in our code.

201
00:10:02,360 --> 00:10:05,920
And then for now with this file added here,

202
00:10:05,920 --> 00:10:08,400
we can go back to another place

203
00:10:08,400 --> 00:10:11,283
where I do wanna establish this connection.

204
00:10:12,220 --> 00:10:15,310
And what could be a good place for doing that?

205
00:10:15,310 --> 00:10:18,480
Well, actually it would be app.js,

206
00:10:18,480 --> 00:10:22,870
where we start this overall node-express server

207
00:10:22,870 --> 00:10:25,650
and where we initialize all our settings

208
00:10:25,650 --> 00:10:29,010
and all our middlewares, that's a great place

209
00:10:29,010 --> 00:10:31,863
for actually also connecting to the database.

210
00:10:32,840 --> 00:10:35,440
And I wanna connect to the database here,

211
00:10:35,440 --> 00:10:37,680
once all the other set up was done.

212
00:10:37,680 --> 00:10:39,220
So here at the bottom, maybe.

213
00:10:39,220 --> 00:10:41,730
There, I wanna connect to my database.

214
00:10:41,730 --> 00:10:45,160
And actually I only wanna start listening here

215
00:10:45,160 --> 00:10:50,160
on port 3000 if that database connection was established.

216
00:10:51,750 --> 00:10:54,200
And to ensure that this is the case,

217
00:10:54,200 --> 00:10:57,270
I'll import my database,

218
00:10:57,270 --> 00:11:00,430
or my db file

219
00:11:00,430 --> 00:11:05,430
by requiring ./data/database like this,

220
00:11:07,170 --> 00:11:11,743
that will actually require this database file.

221
00:11:12,970 --> 00:11:17,870
And then with that here, we can call connectToDatabase,

222
00:11:17,870 --> 00:11:20,390
this one function we're exporting.

223
00:11:20,390 --> 00:11:23,960
And that will then return a promise.

224
00:11:23,960 --> 00:11:28,100
And here we can't necessarily use await,

225
00:11:28,100 --> 00:11:30,913
since we're not inside of an async function here.

226
00:11:32,060 --> 00:11:34,960
Hence, what we can do here is we can use the then method,

227
00:11:34,960 --> 00:11:38,290
which you also learned about in an earlier course section,

228
00:11:38,290 --> 00:11:40,040
which takes a function,

229
00:11:40,040 --> 00:11:42,610
in this case, for example, an anonymous function

230
00:11:42,610 --> 00:11:45,680
that will be executed once this promise resolved,

231
00:11:45,680 --> 00:11:47,393
so once this promise is done.

232
00:11:48,360 --> 00:11:52,460
And then I'll move app.listen inside of this function,

233
00:11:52,460 --> 00:11:55,960
so that app.listen 3000 only executed

234
00:11:55,960 --> 00:11:59,440
once the database connection was established.

235
00:11:59,440 --> 00:12:03,240
Simply to ensure that we only start this web server,

236
00:12:03,240 --> 00:12:05,673
if we have an active database connection.

237
00:12:07,860 --> 00:12:11,920
Now we could also handle the connection in different cases.

238
00:12:11,920 --> 00:12:13,710
We could establish it differently,

239
00:12:13,710 --> 00:12:17,050
and we could also, for example, check if the server crashes

240
00:12:17,050 --> 00:12:18,340
or anything like that.

241
00:12:18,340 --> 00:12:22,520
And in that case, shut down our database connection,

242
00:12:22,520 --> 00:12:25,450
but I don't wanna make things too complex here.

243
00:12:25,450 --> 00:12:27,310
And therefore, for the moment,

244
00:12:27,310 --> 00:12:29,860
I'll stop my running nodemon server

245
00:12:29,860 --> 00:12:32,630
by pressing control + C in this terminal,

246
00:12:32,630 --> 00:12:35,020
and I'll save all files

247
00:12:35,020 --> 00:12:38,100
and then restart this all with npm start.

248
00:12:38,100 --> 00:12:40,420
And since it now is running again,

249
00:12:40,420 --> 00:12:43,780
and I can visit my blog again,

250
00:12:43,780 --> 00:12:45,790
it looks like everything works.

251
00:12:45,790 --> 00:12:47,700
And it looks like indeed we do have

252
00:12:47,700 --> 00:12:49,453
this active connection now.

253
00:12:50,970 --> 00:12:53,270
And therefore, now we established a connection

254
00:12:53,270 --> 00:12:54,560
as a next step.

255
00:12:54,560 --> 00:12:58,210
We can now make sure that we actually query our authors

256
00:12:58,210 --> 00:13:01,010
so that we can pre-populate this dropdown

257
00:13:01,010 --> 00:13:04,643
on the create post page with the author's data.

