Hi everyone welcome back.
So in this video we're going to be building or building but really experimenting with Mongo D.B and
how it can interact with Mongo D-B right from a flask.
So he already built.
Remember here we built to amped up using dog or compose.
Now we want to build a data base here and this database will basically tell us how we can build or how
we can generate this database right.
So how can we interact from this web all here all the way to the Mongo D-B.
Right.
So this is what's what we're going to be understanding in this video.
OK.
So the first thing is less open the whole project so we can see it.
So I'll go here and then search for termino.
So I want to open a new terminal.
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
atom here.
OK.
So let's think about what we need to do first before anything else.
So the first step that we need to do.
So here's the project for us.
Let's just leave it to load.
So let's close this and this and this.
OK.
So here's a project here's what we've been building so far and this is our web and the application up
UI and everything is nice.
OK.
So the first thing is we need to download the the the image file for Mongo D-B.
Right.
So the whole idea of how we are going to be doing this is a couple of steps.
OK.
So first of all we're going to go to to Dr. right because we need an image a darker image or a darker
image.
Right.
So we're going to go to darker hub.
OK.
To get an image for Mongo D-B.
So we're going to go to a dog or.
And then we're going to search for Mongo D-B OK so here's Mungo Eby right.
So click on this one.
So actually there's an official version of it.
So let's go back and there's the official version of Mongo here.
It will go to this one instead.
And then you'll notice there's a there's many differences in the previous video we use three point six
point four so we'll just use that in the video.
OK.
OK so we now we now know that there is a version of Mongo TB and it's three point 6.4.
So how do we down.
Do we add it.
OK Will we do the same thing as we did with flask.
Right.
So we open the terminal.
We go to D-B.
OK.
And then I'm going to touch a dog or file so I have to define a dog or fight.
Right.
So I'll touch a dog or file.
So now the docker file should appear here and then from here I want to say a.
So I'm going to get an image now and it's going to be called Mongo.
And then three point six point four.
OK.
So if you go back here it's let's go back and go down here.
So it's Mongo.
OK.
So let's see Mongo and then some version of Mongo.
Right.
OK.
So let's so it's Mungo and then a tag off the version of the Mongo that you want.
OK.
Hopefully that makes sense.
So we'll get from Ango 3.6 2.4 and that's it.
Basically we just want to get an image of Mongo and this will automatically this this image will automatically
set everything up for you in the database in the Mangieri database.
And so if we go back years now we need to define this in the dark on those file that we talked about.
It has everything.
Right so I go back and then I'm going to insert Now I want to define B.
Right so I want to say DP D-MI is another service and then I can build it from dot slash D-B.
OK so I can build it from from this doc or file over here.
Now another thing is we know that this web here this web application flask depends on the database.
So I'm going to add an extra option here.
I'm going to say links and then I want to do a list and then I'm going to write here D-B.
To me means that this here is very this web service is dependent on this db service so we have to build
this one first and then build the other one.
So hopefully this makes sense.
OK so this should be it.
So we have the first step is that we have the darker image and we've put it in this knocker file so
we can build it.
But then the second thing is well how do we communicate from this year from this application how do
we communicate into the database right away.
And the way we do this is actually to use varchar compose itself because Duguid compose creates a network
for you to connect to the other database.
Now how do we do this.
Well the first thing is I can communicate from bison right away with the database so I need some intermediary
between that.
Right.
So there's actually a library called Pine mombo.
So we're going to add a new library that we get called pi Mongo and by Mongo is as the name might suggest.
It's Python and that Mungo.
So it gives you an interface to deal from.
From Python and to the right of way with the database that you have.
So we're going to need also PI Mongo.
OK so now let's save this and build our project and see what happens.
OK.
So we're going to do pseudo doctor compose build.
And the reason we're not doing up is because we've added some extra stuff here and we've also added
a new doctor file and we've changed the doc or compose.
So all of these things have changed the main file so I need to rebuild my project somewhat a pseudo
Dharker compose build and that is going to start building the process anyway.
Notice here that it goes right away to the step off pulling Mongo D-B from the from the dock or out.
So I'm going to leave this down note and when it finishes downloading I'm going to come back and we
can resume the throw from there.
OK so welcome back.
So after we build the project you notice here that we first downloaded and PI Mongo and installed it
as an image and then we also downloaded and installed a flask flask restful and kind longer here.
So this is what will enable us to communicate with the database in the other file.
OK.
OK so far so good.
Now let's go back to our applications or abduct up why and now we want to somehow access the database
that is in this file over here.
So the way we use pi magoi is well first of all well I will make a very official tutorial on PI Mongo
because you just have to know a few of the functions in it.
And that's you know after that you can refer it to the documentation so we don't need to cover the entirety
off fine.
So the way you would use it is basically you're saying from time Ango import and then you're going to
import the Mongo client.
Now take care of that.
The M is capital and the C is capital cake.
So import Mongo Klein and then you're going to say after you've designed your API you're going to say
you're going to say the next thing I'll do is to initialize a new clients I'm going to say a client
is equal to Mungo client and then in your elbow the client I'm going to say Mungo beads I'm going to
say Mangu Mongo D-B.
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
be the same name as in your doc or compose file so if you notice here I named my database is D-B.
So this D-B must be exactly the same as the one you named in the dog numbers.
So that way I'm telling Mungo Hey communicate with the Mongul D-B X and it's defined from this doctor
compose file over here so we'll know that it's talking about this database over here.
Now the final thing is to add a semi-colon and then to add to 7 0 1 Sep.
Now 270 0 1 7 is the default port that month.
Mungo D-B works on so each Mangat any must have a port and the default default one is 2 7 0 1 0.
So any other port you would have to define it yourself.
But for now we'll just go with the default support for mongered Eubie which is 2 7 0 1 0.
So now this client has basically connected into the database.
Now the next thing we're going to do is to make a database right the first step is to create a database.
I'm going to say the ME is equal to client dot and new D-B.
Ok someone to call my database as and PDB.
OK.
And then the second thing that I do is I want to do a collection's.
So I'm going to say a user no.
So the number of users is equal to D-B.
And then I want to make a new database I'm going to call it user you no.
OK so this variable over here would denote this database D-B user now and or this collection over here.
Now inside this user name I want to insert one or just and just one document is going to be user now.
So let's go back to your user name and then dot insert some going to insert some stuff some stuff and
it's going to be like a Jason File.
And then here I'm going to have a number of users.
And on here I have zero.
So initially I have 0 0 users visiting my web site.
Right now I want to keep track of how many users are visiting my web site.
So what I do is once I have this year I now have a database and a collection and a document that has
the number of users that visit my Web site.
So what I.
What I do is I'll define.
So I define a class of class visit.
OK.
And then I'm going to add to it resource.
As we mentioned earlier and then define get.
So every time my web site is visited.
OK.
The first thing that I want to do is to know how many how many people are already visiting my my web
site already.
So I'm going to say previous number is equal to user no.
So the user in our collection.
That's fine.
And then since it only has one element in it it's going to return this element in an array.
So I'm just going to access the zero element of that array.
And then I also want to access inside this zero.
And I don't want to access the number of users I want to copy this year.
And then I want to access the number of users.
So the previous number is going to have the previous number of users.
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.
And then I'm going to say I want to add I want to replace this number here that whatever existing number
here with this number.
So I have to use the update function if you remember somebody using user in home update.
OK.
And then the first selection criteria is nothing right.
It just update.
Well there's only one document to just update it.
And then with what I'm going to update I'm going to open a pair of curly brackets and then I'm going
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
of users so I'm a copy this year's of the number of users must be.
Or.
OK so let's say this instead of previous number plus equal 1 we're going to call this number.
OK.
So a new number is equal to previous number plus 1.
So now I'm going to update the number of users with the new number.
OK.
So now added one extra to my my database and then I'm going to say use your home.
So so once I updated it.
So this operation has been successful.
Now my database knows how many people.
So I'm going to return the string Hello user and then I'm going to add this drink New Coke.
So I'm going to say hello user 1 Hellblazer to all users 3.
So every user that visits my page is going to know what number he is visiting.
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
and so on.
OK so every time you refresh the page this number should increase.
OK so now we're going to return the string over here hello user and then some string.
Ok so a couple of things left to do.
The first thing is to add this class or this resource into our API.
So we're going to go down and then we're going to say API don't add resource.
OK.
And then we're going to we we named the class your visit.
So I'm going to call it visit and then I'm going to put it at slash.
Hello.
OK.
So if you go to slash Hello you should see this message.
I'm going to save this and then go here Sudoku and desktop Victorio also assume that AUKer compose was
build first and then I'm going to do a pseudo Dharker compose up so hopefully shouldn't be too much
problems.
We have some problem.
See.
OK.
OK so there is just a tiny mistake here.
So here in five senses we're writing this in python.
This train over here must be a string.
So in Lango the client it's OK to not use the string but in this bison here we must use this string
or here.
OK.
So instead of just the key if you remember in Mungo D-B the normal client you can use the key without
any Cope's.
But in here it's a must to use codes for the for the key.
And this is really just a Piscean thing.
OK.
So we're going to save that close.
Kill kill Control-C and then build quickly that up and hopefully this and that shouldn't be too much
problems.
Yeah and we have a running server.
So if we go here cake and we go your and we go we refresh the page at localhost 5000 I-Slash hello for
your refresh we'll get.
Hello user 1.
Now if I refresh again then I have hello user 2 Hello 0 3 4 5 and so on.
So I can keep doing this forever.
I can keep repeating this and then it will increment the number off users every time.
Right.
So I keep getting all the way to 27 so that we can figure out how many times one of the endpoints was
hit.
OK.
So this is a very very simple example of how to use mongered be with your application to keep track
of something.
So in this case we kept track of the number of users in the future video we're going to keep track of
the usernames and passwords right for the system.
So we know what users users are and everything like that.
So for now the whole picture is starting to do you know engrave in your brain about how the whole workflow
of building an API works.
So yeah we'll stop here and I'll see you in the next video.
So until the next video happy Cody.