In this video, let’s connect our application to MongoDB.
But before we get into that, in the previous video, I gave this one to you as a challenge that is
refactor to the account and then the transaction route use MVC design pattern.
If you couldn’t solve it, don’t worry, I have provided a source code for it and better so we will
revisit the controls as we implement the actual logic.
So for this video, let’s go ahead and then connect to MongoDB.
I assume that you have your MongoDB connection string ready if you don’t have it.
I advise you to watch the section and get in connection string.
So with that being said, first step is let’s go ahead and then install Mongoose.
Now Mongoose has finished installing.
Next is let’s restart our server and let’s continue the implementation.
So here we go inside the config folder.
Let’s create one file and call this one as DB connect.
It’s genius.
And inside this fire, let’s require the mongoose package.
The next step is let’s create the function to connect to TV.
So here we go.
And.
And for this it takes in the connection string and this is my connection string.
So I would paste it here as that.
And for this one I want to name my database by using what is called.
A save it.
Great.
So inside the cat, in case something goes wrong, let’s go ahead and then console.log.
The actual error on that we have the message.
And if you couldn’t connect, let’s go ahead and then exit our server that is processed dot exit and
you pass in one for exiting the application.
And here in case everything goes right.
Let’s go ahead and say that DB connected successfully.
All right.
So for this, I want to call the function automatically here without exporting it, meaning that as
soon as I require this file, this function will be run.
So let’s go ahead and then require it inside the server.
And this is how I’m going to require it.
So going to be as required and then goes for this last config.
And inside there we have the DB connect.
Now let’s check our terminal and let’s have a look.
You can see that we have some error connecting.
So let’s look at the connection string.
Well, in case you’re not doing something right, oh, I have to remove the additional code around it,
and that is it.
Let’s check it out and see.
Moment of truth and there we go.
DB Connected successfully, so now we are done with that one.
Guys, before we continue, I have changed the part from 9000 to 8080 because that part, which is 10,000,
is being used by different application on my operating system.
So that is it.
So next step is that let me show you how I’m going to use the extension code MongoDB.
If we install the extension that is MongoDB for VTS code, click on that and here click on Add connection.
And for this, let’s bring in our connection string.
So let me copy and let’s go to the extension and click on Connect.
Click on that.
And here it says that paste the connection string and let me go ahead and then paste that and hit enter.
Now something is going to happen.
Let’s wait a there we go.
You can see that connected.
So let’s click on the extension and here is my database.
So here we can visualize what is going on inside our database right inside Visual Studio code in the
next video.
Let’s go ahead and then model our data for this particular application.