This video.
Let's see how we can connect our application to MongoDB server so that we can start to work with what's
Mongo DB.
So here we go on the MongoDB JS file.
Let's go ahead and require the package we install.
That is the MongoDB from the previous video.
So now let me collapse this and now let me require the MongoDB package.
And on that we have different methods.
So I'm going to structure what we want so required and then the MongoDB package and here we go.
At this point we are not going to use Mongoose, you are going to use purely MongoDB as you move on.
We are going to use Mongoose to make our life easier.
So let's go.
What I want for MongoDB is what we call Mongo client.
Mongo client.
So this one and that is it.
So the next step is we need to have what is called the connection string, meaning the connection string
that we are going to use to connect to MongoDB.
So where can we get connections training after creating account with MongoDB we have access to our connection
string, but let's see example of the connection string.
So this is where we lived and that the process of installing MongoDB and if we scroll down and connect
to MongoDB, let's click on that and let's click on the Connection Guide.
So click on that.
And here we have what is called the connection string.
So what is the connection string?
Well, the connection string is a set of instructions that the driver uses to connect to a MongoDB deployment.
It instruct the driver on how to connect to MongoDB and how it should have why connected.
So let's look at the formats we have MongoDB, which is a protocol, and then the username password
with our credentials.
Remember upon creating we provided username and password and I told you that save the password somewhere
else because you may need it.
And what comes after add is what you call the hostname and then with the connection options.
So how can we get our connection string?
It's pretty easy.
So back to the MongoDB dashboard and then let's click on database.
And next step is we are going to connect application to our application.
So let's click on Connect.
And here you go.
You can see that we have couple of options connect to MongoDB, share, like I said, and connect your
application.
So let's click on the second one, connect your application and here is asking us to select our driver
because we are working with MongoDB.
Let's select the driver and then the version.
Let's select the latest version which is 4.1.
So make sure that you select 4.1 and after that the next step says that add your connection string.
So this is our connection code.
Like we explained before, we have the protocol here, the user name and password, and the other option
is that so click on this one and now back to our application.
Let's create a variable called you are error.
So cost you are error is equal to code and paste the connection string.
So this is same as we have explored before.
The next step is we need to provide the password.
You remember I asked you to work to save your password, but in case you have forgotten, don't worry,
we can get it back.
So the next step is click on database access.
So if you want to add a new user, you can go ahead and do that.
All right.
So the next step is network access of which we have done it when we're configuring our account.
So for this IP address, anyone can have access to our MongoDB, meaning that a person can create can
delete config based on our business logic.
So in case you have forgotten your password, click on the database access and next is click on edit
and here you can edit your password as that generate and copy and that is it.
Well, next step is let's connect our application to MongoDB.
But here is a problem one.
Since we are going to talk to external server, we don't know when the server or the connection will
be successful.
It can happen that our connection will fail or it will be successful.
So we need to handle all these cases.
So we have couple of options to handle this.
One, we can use callback or we can use promise or we can use async await.
Let's get going.
I'm going to use arrow function and the name of my connection going to be as DB connect equal to my
arrow function as that.
And since I'm going to use async await, I'll mark my function as an async and inside a function body,
our brain might try and catch inside a try.
I'm going to implement if everything goes on well and inside the catch, if something goes wrong, I'm
going to display some message.
So now here we go.
The next step is I need to create an instance of my new MongoDB client.
So here we go.
So I'll give it a comment as create new client and make use of cost and client equal to new and make
use of Mongo clients, which is this one.
And for this our pass in the connection string which is this.
So our pass in the you are error and that is it.
So inside here, let's go ahead and connect to DB or MongoDB.
So I'm going to await the client instance on that.
We have a method called connect and as a function or method as that and inside the catch, if something
goes wrong, why not go ahead and console.log the error as that.
The next step is I need to call this function DB connect.
So now save it and let's run the server or the application or the file and let's see if we are going
to see connection successful.
So here inside the try, let's provide a message to the user if the connection was successful.
So I would say that DB.
Connected successfully.
All right, save it.
And now back to the terminal.
Make use of node and then the file is MongoDB.
Hit enter.
And if we see that DB connected successfully.
So at this point we are able to connect our application to MongoDB.
So now it's time to explore more about MongoDB, how we can create record and save it into our MongoDB
account here.
All right, so let's try to change the password and let's see if we're going to get an error.
So I have remove some portion of my password, save it back to the terminal.
Restarted and let's see.
And you can see I have beautiful error, mongo server error, bad authentication meaning that I have
provided that password or username, which is true.
So let me bring it back to defaults and save it.
And let's run the file again.
And here you go.
Please come.
MongoDB connected.
Great.
So now it's time for us to learn more about MongoDB.
The next video we will start with growth operations, how we can create, record, delete, update and
fetch all.
Let's get into that the next video.