WEBVTT

00:05.210 --> 00:07.040
Hi everyone welcome back.

00:07.040 --> 00:14.030
So in this video we're going to be building or building but really experimenting with Mongo D.B and

00:14.030 --> 00:19.150
how it can interact with Mongo D-B right from a flask.

00:19.160 --> 00:20.310
So he already built.

00:20.330 --> 00:24.450
Remember here we built to amped up using dog or compose.

00:24.470 --> 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

00:33.200 --> 00:35.350
we can generate this database right.

00:35.360 --> 00:41.330
So how can we interact from this web all here all the way to the Mongo D-B.

00:41.330 --> 00:41.600
Right.

00:41.600 --> 00:46.090
So this is what's what we're going to be understanding in this video.

00:46.240 --> 00:46.820
OK.

00:46.910 --> 00:50.120
So the first thing is less open the whole project so we can see it.

00:50.120 --> 00:54.460
So I'll go here and then search for termino.

00:54.470 --> 00:57.020
So I want to open a new terminal.

00:57.230 --> 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

01:06.760 --> 01:07.860
atom here.

01:08.260 --> 01:08.860
OK.

01:09.050 --> 01:12.500
So let's think about what we need to do first before anything else.

01:12.500 --> 01:14.230
So the first step that we need to do.

01:14.230 --> 01:15.660
So here's the project for us.

01:15.680 --> 01:17.320
Let's just leave it to load.

01:17.330 --> 01:21.890
So let's close this and this and this.

01:22.220 --> 01:22.720
OK.

01:22.820 --> 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

01:29.030 --> 01:30.390
UI and everything is nice.

01:30.410 --> 01:31.310
OK.

01:31.910 --> 01:39.230
So the first thing is we need to download the the the image file for Mongo D-B.

01:39.240 --> 01:40.020
Right.

01:40.550 --> 01:45.250
So the whole idea of how we are going to be doing this is a couple of steps.

01:45.260 --> 01:45.640
OK.

01:45.770 --> 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

01:53.510 --> 01:53.990
image.

01:53.990 --> 01:54.430
Right.

01:54.560 --> 01:57.080
So we're going to go to darker hub.

01:57.340 --> 01:58.060
OK.

02:00.200 --> 02:03.000
To get an image for Mongo D-B.

02:03.410 --> 02:05.030
So we're going to go to a dog or.

02:05.180 --> 02:12.450
And then we're going to search for Mongo D-B OK so here's Mungo Eby right.

02:12.580 --> 02:14.260
So click on this one.

02:14.530 --> 02:17.400
So actually there's an official version of it.

02:17.510 --> 02:21.040
So let's go back and there's the official version of Mongo here.

02:21.210 --> 02:23.280
It will go to this one instead.

02:23.770 --> 02:29.060
And then you'll notice there's a there's many differences in the previous video we use three point six

02:29.070 --> 02:32.270
point four so we'll just use that in the video.

02:32.320 --> 02:32.920
OK.

02:33.100 --> 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.

02:39.610 --> 02:40.520
So how do we down.

02:40.570 --> 02:41.620
Do we add it.

02:41.860 --> 02:44.790
OK Will we do the same thing as we did with flask.

02:44.800 --> 02:45.240
Right.

02:45.370 --> 02:46.820
So we open the terminal.

02:46.840 --> 02:48.440
We go to D-B.

02:48.520 --> 02:49.170
OK.

02:49.420 --> 02:53.720
And then I'm going to touch a dog or file so I have to define a dog or fight.

02:53.800 --> 02:54.470
Right.

02:54.550 --> 02:56.360
So I'll touch a dog or file.

02:56.500 --> 03:02.080
So now the docker file should appear here and then from here I want to say a.

03:02.080 --> 03:07.300
So I'm going to get an image now and it's going to be called Mongo.

03:07.390 --> 03:10.800
And then three point six point four.

03:10.840 --> 03:11.420
OK.

03:11.620 --> 03:17.600
So if you go back here it's let's go back and go down here.

03:17.800 --> 03:19.250
So it's Mongo.

03:19.300 --> 03:20.180
OK.

03:20.590 --> 03:26.230
So let's see Mongo and then some version of Mongo.

03:26.260 --> 03:26.870
Right.

03:27.850 --> 03:28.410
OK.

03:28.540 --> 03:33.650
So let's so it's Mungo and then a tag off the version of the Mongo that you want.

03:34.060 --> 03:34.340
OK.

03:34.360 --> 03:35.730
Hopefully that makes sense.

03:35.920 --> 03:39.730
So we'll get from Ango 3.6 2.4 and that's it.

03:39.880 --> 03:46.750
Basically we just want to get an image of Mongo and this will automatically this this image will automatically

03:47.620 --> 03:54.010
set everything up for you in the database in the Mangieri database.

03:54.340 --> 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.

04:00.790 --> 04:02.620
It has everything.

04:02.890 --> 04:08.330
Right so I go back and then I'm going to insert Now I want to define B.

04:08.350 --> 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.

04:17.020 --> 04:22.710
OK so I can build it from from this doc or file over here.

04:22.800 --> 04:30.410
Now another thing is we know that this web here this web application flask depends on the database.

04:30.630 --> 04:34.880
So I'm going to add an extra option here.

04:34.890 --> 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.

04:42.320 --> 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

04:50.600 --> 04:53.450
this one first and then build the other one.

04:53.480 --> 04:55.210
So hopefully this makes sense.

04:56.510 --> 04:58.390
OK so this should be it.

04:58.390 --> 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

05:06.100 --> 05:07.080
we can build it.

05:07.360 --> 05:13.390
But then the second thing is well how do we communicate from this year from this application how do

05:13.390 --> 05:16.420
we communicate into the database right away.

05:16.510 --> 05:25.000
And the way we do this is actually to use varchar compose itself because Duguid compose creates a network

05:25.000 --> 05:28.180
for you to connect to the other database.

05:28.180 --> 05:29.310
Now how do we do this.

05:29.350 --> 05:36.610
Well the first thing is I can communicate from bison right away with the database so I need some intermediary

05:36.610 --> 05:37.270
between that.

05:37.270 --> 05:37.800
Right.

05:38.020 --> 05:41.760
So there's actually a library called Pine mombo.

05:41.800 --> 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.

05:48.380 --> 05:50.660
It's Python and that Mungo.

05:50.770 --> 05:53.760
So it gives you an interface to deal from.

05:53.890 --> 05:58.430
From Python and to the right of way with the database that you have.

05:58.540 --> 06:00.780
So we're going to need also PI Mongo.

06:00.820 --> 06:05.670
OK so now let's save this and build our project and see what happens.

06:05.740 --> 06:06.840
OK.

06:07.840 --> 06:11.510
So we're going to do pseudo doctor compose build.

06:11.530 --> 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

06:16.770 --> 06:19.940
a new doctor file and we've changed the doc or compose.

06:19.960 --> 06:25.270
So all of these things have changed the main file so I need to rebuild my project somewhat a pseudo

06:25.270 --> 06:30.400
Dharker compose build and that is going to start building the process anyway.

06:30.430 --> 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.

06:38.380 --> 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

06:43.390 --> 06:45.810
can resume the throw from there.

06:46.060 --> 06:47.400
OK so welcome back.

06:47.560 --> 06:54.460
So after we build the project you notice here that we first downloaded and PI Mongo and installed it

06:54.520 --> 07:02.400
as an image and then we also downloaded and installed a flask flask restful and kind longer here.

07:02.410 --> 07:07.140
So this is what will enable us to communicate with the database in the other file.

07:07.210 --> 07:07.860
OK.

07:09.120 --> 07:10.570
OK so far so good.

07:10.600 --> 07:18.810
Now let's go back to our applications or abduct up why and now we want to somehow access the database

07:18.840 --> 07:21.690
that is in this file over here.

07:21.690 --> 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

07:29.220 --> 07:32.520
because you just have to know a few of the functions in it.

07:32.670 --> 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

07:38.160 --> 07:39.560
off fine.

07:39.770 --> 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

07:47.130 --> 07:49.590
import the Mongo client.

07:49.590 --> 07:50.520
Now take care of that.

07:50.520 --> 07:54.070
The M is capital and the C is capital cake.

07:54.420 --> 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

08:01.140 --> 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

08:08.700 --> 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

08:15.960 --> 08:19.600
say Mangu Mongo D-B.

08:19.770 --> 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

08:27.240 --> 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.

08:33.210 --> 08:38.160
So this D-B must be exactly the same as the one you named in the dog numbers.

08:38.220 --> 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

08:46.260 --> 08:51.010
compose file over here so we'll know that it's talking about this database over here.

08:51.030 --> 08:55.670
Now the final thing is to add a semi-colon and then to add to 7 0 1 Sep.

08:55.680 --> 09:00.800
Now 270 0 1 7 is the default port that month.

09:00.800 --> 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.

09:09.330 --> 09:14.330
So any other port you would have to define it yourself.

09:14.340 --> 09:19.750
But for now we'll just go with the default support for mongered Eubie which is 2 7 0 1 0.

09:19.980 --> 09:24.870
So now this client has basically connected into the database.

09:24.870 --> 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.

09:30.600 --> 09:37.530
I'm going to say the ME is equal to client dot and new D-B.

09:37.590 --> 09:41.120
Ok someone to call my database as and PDB.

09:41.130 --> 09:41.980
OK.

09:42.390 --> 09:45.540
And then the second thing that I do is I want to do a collection's.

09:45.720 --> 09:48.550
So I'm going to say a user no.

09:48.720 --> 09:52.010
So the number of users is equal to D-B.

09:52.020 --> 09:56.260
And then I want to make a new database I'm going to call it user you no.

09:56.460 --> 10:04.540
OK so this variable over here would denote this database D-B user now and or this collection over here.

10:04.580 --> 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.

10:11.610 --> 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

10:20.070 --> 10:22.420
it's going to be like a Jason File.

10:22.800 --> 10:28.060
And then here I'm going to have a number of users.

10:28.860 --> 10:30.540
And on here I have zero.

10:30.540 --> 10:34.940
So initially I have 0 0 users visiting my web site.

10:34.960 --> 10:39.590
Right now I want to keep track of how many users are visiting my web site.

10:39.810 --> 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

10:46.110 --> 10:49.220
the number of users that visit my Web site.

10:49.410 --> 10:50.170
So what I.

10:50.220 --> 10:52.520
What I do is I'll define.

10:52.530 --> 10:55.910
So I define a class of class visit.

10:55.980 --> 10:56.650
OK.

10:56.940 --> 10:59.330
And then I'm going to add to it resource.

10:59.400 --> 11:02.410
As we mentioned earlier and then define get.

11:02.410 --> 11:05.540
So every time my web site is visited.

11:05.910 --> 11:06.620
OK.

11:06.750 --> 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

11:13.080 --> 11:14.080
site already.

11:14.100 --> 11:20.590
So I'm going to say previous number is equal to user no.

11:20.610 --> 11:23.160
So the user in our collection.

11:23.160 --> 11:24.510
That's fine.

11:24.540 --> 11:31.180
And then since it only has one element in it it's going to return this element in an array.

11:31.260 --> 11:35.030
So I'm just going to access the zero element of that array.

11:35.850 --> 11:39.390
And then I also want to access inside this zero.

11:39.440 --> 11:43.080
And I don't want to access the number of users I want to copy this year.

11:43.080 --> 11:45.310
And then I want to access the number of users.

11:45.540 --> 11:49.530
So the previous number is going to have the previous number of users.

11:49.530 --> 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.

11:57.690 --> 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

12:04.530 --> 12:06.000
here with this number.

12:06.000 --> 12:12.230
So I have to use the update function if you remember somebody using user in home update.

12:12.330 --> 12:13.100
OK.

12:13.350 --> 12:16.300
And then the first selection criteria is nothing right.

12:16.300 --> 12:17.430
It just update.

12:17.460 --> 12:20.440
Well there's only one document to just update it.

12:20.640 --> 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

12:26.280 --> 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

12:36.000 --> 12:40.490
of users so I'm a copy this year's of the number of users must be.

12:40.620 --> 12:41.410
Or.

12:41.790 --> 12:47.520
OK so let's say this instead of previous number plus equal 1 we're going to call this number.

12:47.550 --> 12:48.180
OK.

12:48.300 --> 12:52.840
So a new number is equal to previous number plus 1.

12:53.190 --> 12:58.090
So now I'm going to update the number of users with the new number.

12:58.140 --> 12:59.540
OK.

13:00.120 --> 13:07.970
So now added one extra to my my database and then I'm going to say use your home.

13:08.060 --> 13:09.680
So so once I updated it.

13:09.680 --> 13:11.930
So this operation has been successful.

13:11.990 --> 13:14.250
Now my database knows how many people.

13:14.500 --> 13:24.440
So I'm going to return the string Hello user and then I'm going to add this drink New Coke.

13:24.440 --> 13:27.760
So I'm going to say hello user 1 Hellblazer to all users 3.

13:27.830 --> 13:32.670
So every user that visits my page is going to know what number he is visiting.

13:32.730 --> 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

13:39.290 --> 13:40.010
and so on.

13:40.100 --> 13:44.020
OK so every time you refresh the page this number should increase.

13:44.180 --> 13:49.870
OK so now we're going to return the string over here hello user and then some string.

13:50.360 --> 13:52.430
Ok so a couple of things left to do.

13:52.430 --> 13:56.780
The first thing is to add this class or this resource into our API.

13:56.990 --> 14:01.170
So we're going to go down and then we're going to say API don't add resource.

14:01.250 --> 14:01.920
OK.

14:02.030 --> 14:04.880
And then we're going to we we named the class your visit.

14:05.000 --> 14:09.830
So I'm going to call it visit and then I'm going to put it at slash.

14:09.830 --> 14:10.460
Hello.

14:10.640 --> 14:11.520
OK.

14:11.900 --> 14:15.870
So if you go to slash Hello you should see this message.

14:16.070 --> 14:24.890
I'm going to save this and then go here Sudoku and desktop Victorio also assume that AUKer compose was

14:24.920 --> 14:33.810
build first and then I'm going to do a pseudo Dharker compose up so hopefully shouldn't be too much

14:33.810 --> 14:34.980
problems.

14:35.070 --> 14:36.620
We have some problem.

14:36.700 --> 14:37.460
See.

14:37.740 --> 14:38.390
OK.

14:38.460 --> 14:41.370
OK so there is just a tiny mistake here.

14:41.370 --> 14:45.850
So here in five senses we're writing this in python.

14:45.870 --> 14:48.150
This train over here must be a string.

14:48.150 --> 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

14:54.530 --> 14:55.130
or here.

14:55.170 --> 14:55.770
OK.

14:55.770 --> 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

15:02.970 --> 15:03.760
any Cope's.

15:03.780 --> 15:08.790
But in here it's a must to use codes for the for the key.

15:08.820 --> 15:11.130
And this is really just a Piscean thing.

15:11.360 --> 15:12.000
OK.

15:12.240 --> 15:14.840
So we're going to save that close.

15:15.120 --> 15:23.680
Kill kill Control-C and then build quickly that up and hopefully this and that shouldn't be too much

15:23.710 --> 15:24.600
problems.

15:24.810 --> 15:26.450
Yeah and we have a running server.

15:26.650 --> 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

15:35.200 --> 15:37.070
your refresh we'll get.

15:37.090 --> 15:38.350
Hello user 1.

15:38.370 --> 15:45.260
Now if I refresh again then I have hello user 2 Hello 0 3 4 5 and so on.

15:45.310 --> 15:47.920
So I can keep doing this forever.

15:48.610 --> 15:54.130
I can keep repeating this and then it will increment the number off users every time.

15:54.130 --> 15:54.510
Right.

15:54.640 --> 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

16:01.510 --> 16:01.880
hit.

16:01.930 --> 16:02.430
OK.

16:02.560 --> 16:10.450
So this is a very very simple example of how to use mongered be with your application to keep track

16:10.450 --> 16:11.320
of something.

16:11.320 --> 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

16:16.960 --> 16:20.010
the usernames and passwords right for the system.

16:20.080 --> 16:25.000
So we know what users users are and everything like that.

16:25.030 --> 16:31.620
So for now the whole picture is starting to do you know engrave in your brain about how the whole workflow

16:31.670 --> 16:34.340
of building an API works.

16:34.360 --> 16:37.700
So yeah we'll stop here and I'll see you in the next video.

16:37.900 --> 16:40.000
So until the next video happy Cody.
