﻿1
00:00:05,210 --> 00:00:07,040
Hi everyone welcome back.

2
00:00:07,040 --> 00:00:14,030
So in this video we're going to be building or building but really experimenting with Mongo D.B and

3
00:00:14,030 --> 00:00:19,150
how it can interact with Mongo D-B right from a flask.

4
00:00:19,160 --> 00:00:20,310
So he already built.

5
00:00:20,330 --> 00:00:24,450
Remember here we built to amped up using dog or compose.

6
00:00:24,470 --> 00:00:33,200
Now we want to build a data base here and this database will basically tell us how we can build or how

7
00:00:33,200 --> 00:00:35,350
we can generate this database right.

8
00:00:35,360 --> 00:00:41,330
So how can we interact from this web all here all the way to the Mongo D-B.

9
00:00:41,330 --> 00:00:41,600
Right.

10
00:00:41,600 --> 00:00:46,090
So this is what's what we're going to be understanding in this video.

11
00:00:46,240 --> 00:00:46,820
OK.

12
00:00:46,910 --> 00:00:50,120
So the first thing is less open the whole project so we can see it.

13
00:00:50,120 --> 00:00:54,460
So I'll go here and then search for termino.

14
00:00:54,470 --> 00:00:57,020
So I want to open a new terminal.

15
00:00:57,230 --> 00:01:06,650
So let's first zoom in and then I'll go to the desktop and then do a tutorial one and then I'll open

16
00:01:06,760 --> 00:01:07,860
atom here.

17
00:01:08,260 --> 00:01:08,860
OK.

18
00:01:09,050 --> 00:01:12,500
So let's think about what we need to do first before anything else.

19
00:01:12,500 --> 00:01:14,230
So the first step that we need to do.

20
00:01:14,230 --> 00:01:15,660
So here's the project for us.

21
00:01:15,680 --> 00:01:17,320
Let's just leave it to load.

22
00:01:17,330 --> 00:01:21,890
So let's close this and this and this.

23
00:01:22,220 --> 00:01:22,720
OK.

24
00:01:22,820 --> 00:01:29,030
So here's a project here's what we've been building so far and this is our web and the application up

25
00:01:29,030 --> 00:01:30,390
UI and everything is nice.

26
00:01:30,410 --> 00:01:31,310
OK.

27
00:01:31,910 --> 00:01:39,230
So the first thing is we need to download the the the image file for Mongo D-B.

28
00:01:39,240 --> 00:01:40,020
Right.

29
00:01:40,550 --> 00:01:45,250
So the whole idea of how we are going to be doing this is a couple of steps.

30
00:01:45,260 --> 00:01:45,640
OK.

31
00:01:45,770 --> 00:01:53,490
So first of all we're going to go to to Dr. right because we need an image a darker image or a darker

32
00:01:53,510 --> 00:01:53,990
image.

33
00:01:53,990 --> 00:01:54,430
Right.

34
00:01:54,560 --> 00:01:57,080
So we're going to go to darker hub.

35
00:01:57,340 --> 00:01:58,060
OK.

36
00:02:00,200 --> 00:02:03,000
To get an image for Mongo D-B.

37
00:02:03,410 --> 00:02:05,030
So we're going to go to a dog or.

38
00:02:05,180 --> 00:02:12,450
And then we're going to search for Mongo D-B OK so here's Mungo Eby right.

39
00:02:12,580 --> 00:02:14,260
So click on this one.

40
00:02:14,530 --> 00:02:17,400
So actually there's an official version of it.

41
00:02:17,510 --> 00:02:21,040
So let's go back and there's the official version of Mongo here.

42
00:02:21,210 --> 00:02:23,280
It will go to this one instead.

43
00:02:23,770 --> 00:02:29,060
And then you'll notice there's a there's many differences in the previous video we use three point six

44
00:02:29,070 --> 00:02:32,270
point four so we'll just use that in the video.

45
00:02:32,320 --> 00:02:32,920
OK.

46
00:02:33,100 --> 00:02:39,590
OK so we now we now know that there is a version of Mongo TB and it's three point 6.4.

47
00:02:39,610 --> 00:02:40,520
So how do we down.

48
00:02:40,570 --> 00:02:41,620
Do we add it.

49
00:02:41,860 --> 00:02:44,790
OK Will we do the same thing as we did with flask.

50
00:02:44,800 --> 00:02:45,240
Right.

51
00:02:45,370 --> 00:02:46,820
So we open the terminal.

52
00:02:46,840 --> 00:02:48,440
We go to D-B.

53
00:02:48,520 --> 00:02:49,170
OK.

54
00:02:49,420 --> 00:02:53,720
And then I'm going to touch a dog or file so I have to define a dog or fight.

55
00:02:53,800 --> 00:02:54,470
Right.

56
00:02:54,550 --> 00:02:56,360
So I'll touch a dog or file.

57
00:02:56,500 --> 00:03:02,080
So now the docker file should appear here and then from here I want to say a.

58
00:03:02,080 --> 00:03:07,300
So I'm going to get an image now and it's going to be called Mongo.

59
00:03:07,390 --> 00:03:10,800
And then three point six point four.

60
00:03:10,840 --> 00:03:11,420
OK.

61
00:03:11,620 --> 00:03:17,600
So if you go back here it's let's go back and go down here.

62
00:03:17,800 --> 00:03:19,250
So it's Mongo.

63
00:03:19,300 --> 00:03:20,180
OK.

64
00:03:20,590 --> 00:03:26,230
So let's see Mongo and then some version of Mongo.

65
00:03:26,260 --> 00:03:26,870
Right.

66
00:03:27,850 --> 00:03:28,410
OK.

67
00:03:28,540 --> 00:03:33,650
So let's so it's Mungo and then a tag off the version of the Mongo that you want.

68
00:03:34,060 --> 00:03:34,340
OK.

69
00:03:34,360 --> 00:03:35,730
Hopefully that makes sense.

70
00:03:35,920 --> 00:03:39,730
So we'll get from Ango 3.6 2.4 and that's it.

71
00:03:39,880 --> 00:03:46,750
Basically we just want to get an image of Mongo and this will automatically this this image will automatically

72
00:03:47,620 --> 00:03:54,010
set everything up for you in the database in the Mangieri database.

73
00:03:54,340 --> 00:04:00,790
And so if we go back years now we need to define this in the dark on those file that we talked about.

74
00:04:00,790 --> 00:04:02,620
It has everything.

75
00:04:02,890 --> 00:04:08,330
Right so I go back and then I'm going to insert Now I want to define B.

76
00:04:08,350 --> 00:04:16,990
Right so I want to say DP D-MI is another service and then I can build it from dot slash D-B.

77
00:04:17,020 --> 00:04:22,710
OK so I can build it from from this doc or file over here.

78
00:04:22,800 --> 00:04:30,410
Now another thing is we know that this web here this web application flask depends on the database.

79
00:04:30,630 --> 00:04:34,880
So I'm going to add an extra option here.

80
00:04:34,890 --> 00:04:42,090
I'm going to say links and then I want to do a list and then I'm going to write here D-B.

81
00:04:42,320 --> 00:04:50,600
To me means that this here is very this web service is dependent on this db service so we have to build

82
00:04:50,600 --> 00:04:53,450
this one first and then build the other one.

83
00:04:53,480 --> 00:04:55,210
So hopefully this makes sense.

84
00:04:56,510 --> 00:04:58,390
OK so this should be it.

85
00:04:58,390 --> 00:05:06,100
So we have the first step is that we have the darker image and we've put it in this knocker file so

86
00:05:06,100 --> 00:05:07,080
we can build it.

87
00:05:07,360 --> 00:05:13,390
But then the second thing is well how do we communicate from this year from this application how do

88
00:05:13,390 --> 00:05:16,420
we communicate into the database right away.

89
00:05:16,510 --> 00:05:25,000
And the way we do this is actually to use varchar compose itself because Duguid compose creates a network

90
00:05:25,000 --> 00:05:28,180
for you to connect to the other database.

91
00:05:28,180 --> 00:05:29,310
Now how do we do this.

92
00:05:29,350 --> 00:05:36,610
Well the first thing is I can communicate from bison right away with the database so I need some intermediary

93
00:05:36,610 --> 00:05:37,270
between that.

94
00:05:37,270 --> 00:05:37,800
Right.

95
00:05:38,020 --> 00:05:41,760
So there's actually a library called Pine mombo.

96
00:05:41,800 --> 00:05:48,370
So we're going to add a new library that we get called pi Mongo and by Mongo is as the name might suggest.

97
00:05:48,380 --> 00:05:50,660
It's Python and that Mungo.

98
00:05:50,770 --> 00:05:53,760
So it gives you an interface to deal from.

99
00:05:53,890 --> 00:05:58,430
From Python and to the right of way with the database that you have.

100
00:05:58,540 --> 00:06:00,780
So we're going to need also PI Mongo.

101
00:06:00,820 --> 00:06:05,670
OK so now let's save this and build our project and see what happens.

102
00:06:05,740 --> 00:06:06,840
OK.

103
00:06:07,840 --> 00:06:11,510
So we're going to do pseudo doctor compose build.

104
00:06:11,530 --> 00:06:16,770
And the reason we're not doing up is because we've added some extra stuff here and we've also added

105
00:06:16,770 --> 00:06:19,940
a new doctor file and we've changed the doc or compose.

106
00:06:19,960 --> 00:06:25,270
So all of these things have changed the main file so I need to rebuild my project somewhat a pseudo

107
00:06:25,270 --> 00:06:30,400
Dharker compose build and that is going to start building the process anyway.

108
00:06:30,430 --> 00:06:38,160
Notice here that it goes right away to the step off pulling Mongo D-B from the from the dock or out.

109
00:06:38,380 --> 00:06:43,390
So I'm going to leave this down note and when it finishes downloading I'm going to come back and we

110
00:06:43,390 --> 00:06:45,810
can resume the throw from there.

111
00:06:46,060 --> 00:06:47,400
OK so welcome back.

112
00:06:47,560 --> 00:06:54,460
So after we build the project you notice here that we first downloaded and PI Mongo and installed it

113
00:06:54,520 --> 00:07:02,400
as an image and then we also downloaded and installed a flask flask restful and kind longer here.

114
00:07:02,410 --> 00:07:07,140
So this is what will enable us to communicate with the database in the other file.

115
00:07:07,210 --> 00:07:07,860
OK.

116
00:07:09,120 --> 00:07:10,570
OK so far so good.

117
00:07:10,600 --> 00:07:18,810
Now let's go back to our applications or abduct up why and now we want to somehow access the database

118
00:07:18,840 --> 00:07:21,690
that is in this file over here.

119
00:07:21,690 --> 00:07:29,220
So the way we use pi magoi is well first of all well I will make a very official tutorial on PI Mongo

120
00:07:29,220 --> 00:07:32,520
because you just have to know a few of the functions in it.

121
00:07:32,670 --> 00:07:38,160
And that's you know after that you can refer it to the documentation so we don't need to cover the entirety

122
00:07:38,160 --> 00:07:39,560
off fine.

123
00:07:39,770 --> 00:07:47,130
So the way you would use it is basically you're saying from time Ango import and then you're going to

124
00:07:47,130 --> 00:07:49,590
import the Mongo client.

125
00:07:49,590 --> 00:07:50,520
Now take care of that.

126
00:07:50,520 --> 00:07:54,070
The M is capital and the C is capital cake.

127
00:07:54,420 --> 00:08:00,330
So import Mongo Klein and then you're going to say after you've designed your API you're going to say

128
00:08:01,140 --> 00:08:08,670
you're going to say the next thing I'll do is to initialize a new clients I'm going to say a client

129
00:08:08,700 --> 00:08:15,960
is equal to Mungo client and then in your elbow the client I'm going to say Mungo beads I'm going to

130
00:08:15,960 --> 00:08:19,600
say Mangu Mongo D-B.

131
00:08:19,770 --> 00:08:27,240
And then the sign two dots and then two dashes slashes and then I'm going to say D.B and this D-MI should

132
00:08:27,240 --> 00:08:33,160
be the same name as in your doc or compose file so if you notice here I named my database is D-B.

133
00:08:33,210 --> 00:08:38,160
So this D-B must be exactly the same as the one you named in the dog numbers.

134
00:08:38,220 --> 00:08:46,240
So that way I'm telling Mungo Hey communicate with the Mongul D-B X and it's defined from this doctor

135
00:08:46,260 --> 00:08:51,010
compose file over here so we'll know that it's talking about this database over here.

136
00:08:51,030 --> 00:08:55,670
Now the final thing is to add a semi-colon and then to add to 7 0 1 Sep.

137
00:08:55,680 --> 00:09:00,800
Now 270 0 1 7 is the default port that month.

138
00:09:00,800 --> 00:09:09,230
Mungo D-B works on so each Mangat any must have a port and the default default one is 2 7 0 1 0.

139
00:09:09,330 --> 00:09:14,330
So any other port you would have to define it yourself.

140
00:09:14,340 --> 00:09:19,750
But for now we'll just go with the default support for mongered Eubie which is 2 7 0 1 0.

141
00:09:19,980 --> 00:09:24,870
So now this client has basically connected into the database.

142
00:09:24,870 --> 00:09:30,590
Now the next thing we're going to do is to make a database right the first step is to create a database.

143
00:09:30,600 --> 00:09:37,530
I'm going to say the ME is equal to client dot and new D-B.

144
00:09:37,590 --> 00:09:41,120
Ok someone to call my database as and PDB.

145
00:09:41,130 --> 00:09:41,980
OK.

146
00:09:42,390 --> 00:09:45,540
And then the second thing that I do is I want to do a collection's.

147
00:09:45,720 --> 00:09:48,550
So I'm going to say a user no.

148
00:09:48,720 --> 00:09:52,010
So the number of users is equal to D-B.

149
00:09:52,020 --> 00:09:56,260
And then I want to make a new database I'm going to call it user you no.

150
00:09:56,460 --> 00:10:04,540
OK so this variable over here would denote this database D-B user now and or this collection over here.

151
00:10:04,580 --> 00:10:11,550
Now inside this user name I want to insert one or just and just one document is going to be user now.

152
00:10:11,610 --> 00:10:20,010
So let's go back to your user name and then dot insert some going to insert some stuff some stuff and

153
00:10:20,070 --> 00:10:22,420
it's going to be like a Jason File.

154
00:10:22,800 --> 00:10:28,060
And then here I'm going to have a number of users.

155
00:10:28,860 --> 00:10:30,540
And on here I have zero.

156
00:10:30,540 --> 00:10:34,940
So initially I have 0 0 users visiting my web site.

157
00:10:34,960 --> 00:10:39,590
Right now I want to keep track of how many users are visiting my web site.

158
00:10:39,810 --> 00:10:46,110
So what I do is once I have this year I now have a database and a collection and a document that has

159
00:10:46,110 --> 00:10:49,220
the number of users that visit my Web site.

160
00:10:49,410 --> 00:10:50,170
So what I.

161
00:10:50,220 --> 00:10:52,520
What I do is I'll define.

162
00:10:52,530 --> 00:10:55,910
So I define a class of class visit.

163
00:10:55,980 --> 00:10:56,650
OK.

164
00:10:56,940 --> 00:10:59,330
And then I'm going to add to it resource.

165
00:10:59,400 --> 00:11:02,410
As we mentioned earlier and then define get.

166
00:11:02,410 --> 00:11:05,540
So every time my web site is visited.

167
00:11:05,910 --> 00:11:06,620
OK.

168
00:11:06,750 --> 00:11:12,990
The first thing that I want to do is to know how many how many people are already visiting my my web

169
00:11:13,080 --> 00:11:14,080
site already.

170
00:11:14,100 --> 00:11:20,590
So I'm going to say previous number is equal to user no.

171
00:11:20,610 --> 00:11:23,160
So the user in our collection.

172
00:11:23,160 --> 00:11:24,510
That's fine.

173
00:11:24,540 --> 00:11:31,180
And then since it only has one element in it it's going to return this element in an array.

174
00:11:31,260 --> 00:11:35,030
So I'm just going to access the zero element of that array.

175
00:11:35,850 --> 00:11:39,390
And then I also want to access inside this zero.

176
00:11:39,440 --> 00:11:43,080
And I don't want to access the number of users I want to copy this year.

177
00:11:43,080 --> 00:11:45,310
And then I want to access the number of users.

178
00:11:45,540 --> 00:11:49,530
So the previous number is going to have the previous number of users.

179
00:11:49,530 --> 00:11:57,210
Now I want to say a previous number plus equal to 1 so I'll add 1 to it because I just got a new visitor.

180
00:11:57,690 --> 00:12:04,530
And then I'm going to say I want to add I want to replace this number here that whatever existing number

181
00:12:04,530 --> 00:12:06,000
here with this number.

182
00:12:06,000 --> 00:12:12,230
So I have to use the update function if you remember somebody using user in home update.

183
00:12:12,330 --> 00:12:13,100
OK.

184
00:12:13,350 --> 00:12:16,300
And then the first selection criteria is nothing right.

185
00:12:16,300 --> 00:12:17,430
It just update.

186
00:12:17,460 --> 00:12:20,440
Well there's only one document to just update it.

187
00:12:20,640 --> 00:12:26,280
And then with what I'm going to update I'm going to open a pair of curly brackets and then I'm going

188
00:12:26,280 --> 00:12:36,000
to say dollar set and then two dots to specify what I'm going to set and then I'm going to set the number

189
00:12:36,000 --> 00:12:40,490
of users so I'm a copy this year's of the number of users must be.

190
00:12:40,620 --> 00:12:41,410
Or.

191
00:12:41,790 --> 00:12:47,520
OK so let's say this instead of previous number plus equal 1 we're going to call this number.

192
00:12:47,550 --> 00:12:48,180
OK.

193
00:12:48,300 --> 00:12:52,840
So a new number is equal to previous number plus 1.

194
00:12:53,190 --> 00:12:58,090
So now I'm going to update the number of users with the new number.

195
00:12:58,140 --> 00:12:59,540
OK.

196
00:13:00,120 --> 00:13:07,970
So now added one extra to my my database and then I'm going to say use your home.

197
00:13:08,060 --> 00:13:09,680
So so once I updated it.

198
00:13:09,680 --> 00:13:11,930
So this operation has been successful.

199
00:13:11,990 --> 00:13:14,250
Now my database knows how many people.

200
00:13:14,500 --> 00:13:24,440
So I'm going to return the string Hello user and then I'm going to add this drink New Coke.

201
00:13:24,440 --> 00:13:27,760
So I'm going to say hello user 1 Hellblazer to all users 3.

202
00:13:27,830 --> 00:13:32,670
So every user that visits my page is going to know what number he is visiting.

203
00:13:32,730 --> 00:13:39,290
So if he's the 13th person to visit my website say he's going to say hello use your one or two or three

204
00:13:39,290 --> 00:13:40,010
and so on.

205
00:13:40,100 --> 00:13:44,020
OK so every time you refresh the page this number should increase.

206
00:13:44,180 --> 00:13:49,870
OK so now we're going to return the string over here hello user and then some string.

207
00:13:50,360 --> 00:13:52,430
Ok so a couple of things left to do.

208
00:13:52,430 --> 00:13:56,780
The first thing is to add this class or this resource into our API.

209
00:13:56,990 --> 00:14:01,170
So we're going to go down and then we're going to say API don't add resource.

210
00:14:01,250 --> 00:14:01,920
OK.

211
00:14:02,030 --> 00:14:04,880
And then we're going to we we named the class your visit.

212
00:14:05,000 --> 00:14:09,830
So I'm going to call it visit and then I'm going to put it at slash.

213
00:14:09,830 --> 00:14:10,460
Hello.

214
00:14:10,640 --> 00:14:11,520
OK.

215
00:14:11,900 --> 00:14:15,870
So if you go to slash Hello you should see this message.

216
00:14:16,070 --> 00:14:24,890
I'm going to save this and then go here Sudoku and desktop Victorio also assume that AUKer compose was

217
00:14:24,920 --> 00:14:33,810
build first and then I'm going to do a pseudo Dharker compose up so hopefully shouldn't be too much

218
00:14:33,810 --> 00:14:34,980
problems.

219
00:14:35,070 --> 00:14:36,620
We have some problem.

220
00:14:36,700 --> 00:14:37,460
See.

221
00:14:37,740 --> 00:14:38,390
OK.

222
00:14:38,460 --> 00:14:41,370
OK so there is just a tiny mistake here.

223
00:14:41,370 --> 00:14:45,850
So here in five senses we're writing this in python.

224
00:14:45,870 --> 00:14:48,150
This train over here must be a string.

225
00:14:48,150 --> 00:14:54,530
So in Lango the client it's OK to not use the string but in this bison here we must use this string

226
00:14:54,530 --> 00:14:55,130
or here.

227
00:14:55,170 --> 00:14:55,770
OK.

228
00:14:55,770 --> 00:15:02,910
So instead of just the key if you remember in Mungo D-B the normal client you can use the key without

229
00:15:02,970 --> 00:15:03,760
any Cope's.

230
00:15:03,780 --> 00:15:08,790
But in here it's a must to use codes for the for the key.

231
00:15:08,820 --> 00:15:11,130
And this is really just a Piscean thing.

232
00:15:11,360 --> 00:15:12,000
OK.

233
00:15:12,240 --> 00:15:14,840
So we're going to save that close.

234
00:15:15,120 --> 00:15:23,680
Kill kill Control-C and then build quickly that up and hopefully this and that shouldn't be too much

235
00:15:23,710 --> 00:15:24,600
problems.

236
00:15:24,810 --> 00:15:26,450
Yeah and we have a running server.

237
00:15:26,650 --> 00:15:35,160
So if we go here cake and we go your and we go we refresh the page at localhost 5000 I-Slash hello for

238
00:15:35,200 --> 00:15:37,070
your refresh we'll get.

239
00:15:37,090 --> 00:15:38,350
Hello user 1.

240
00:15:38,370 --> 00:15:45,260
Now if I refresh again then I have hello user 2 Hello 0 3 4 5 and so on.

241
00:15:45,310 --> 00:15:47,920
So I can keep doing this forever.

242
00:15:48,610 --> 00:15:54,130
I can keep repeating this and then it will increment the number off users every time.

243
00:15:54,130 --> 00:15:54,510
Right.

244
00:15:54,640 --> 00:16:01,510
So I keep getting all the way to 27 so that we can figure out how many times one of the endpoints was

245
00:16:01,510 --> 00:16:01,880
hit.

246
00:16:01,930 --> 00:16:02,430
OK.

247
00:16:02,560 --> 00:16:10,450
So this is a very very simple example of how to use mongered be with your application to keep track

248
00:16:10,450 --> 00:16:11,320
of something.

249
00:16:11,320 --> 00:16:16,910
So in this case we kept track of the number of users in the future video we're going to keep track of

250
00:16:16,960 --> 00:16:20,010
the usernames and passwords right for the system.

251
00:16:20,080 --> 00:16:25,000
So we know what users users are and everything like that.

252
00:16:25,030 --> 00:16:31,620
So for now the whole picture is starting to do you know engrave in your brain about how the whole workflow

253
00:16:31,670 --> 00:16:34,340
of building an API works.

254
00:16:34,360 --> 00:16:37,700
So yeah we'll stop here and I'll see you in the next video.

255
00:16:37,900 --> 00:16:40,000
So until the next video happy Cody.


