Welcome back.
Let's get started with Mongoose.
So to get started with Mongoose first, unless we connect our application to Mongoose.
So in this video, we are going to learn how we can connect our application or express application to
Mongoose.
So let's start it.
The first step is we need to create a folder for this particular project by making use of m.K, d,
i r and the name of the folder our Carmine RS Mongoose Guide.
Hit Enter next is let me see the into the mongoose guide.
Let me open within my Visual Studio code and I have it open here as that.
The next step is how can we install Mongoose?
So if you go to Mongoose Gamescom, this is the official website for Mongoose and this is the official
website for Mongoose.
Lucky for us, Mongoose is being hoisted on NTP and meaning we can install Mongoose for NPM if we search
for Mongoose and PM.
You're going to see this particular page.
Let's read this description.
It says that Mongoose is a MongoDB object modeling to design to work in asynchronous environment.
And lucky for us, Mongoose support both promises and callbacks, meaning that we can make use of async,
await promises or callbacks.
So let's see how.
And down here you can see how popular this package is, about more than 1 million downloads per week.
So let's go ahead and install Mongoose.
Well, for us to be on the same pipeline, make sure you install the exact version of Mongoose.
That is 6.5.2.
So let's go ahead and then install it.
So we are going to work with Mongoose alone without using Xpress and after that we will combine express
to interact with Mongoose also.
So back to the terminal or better stuff, you can use the built in terminal to issue the command.
So let me come here and go ahead and install Mongoose and p m Install Mongoose version 6.5.3.
Let me confirm again the exact version.
Exactly.
So let me go ahead and then install Mongoose.
Now Mongoose has finished installing.
So how can we run the application?
Let's go ahead and create one file core server or mongoose.
Let's make it as Mongoose dart js.
Ain't your going to write everything on one file?
If you go to the official website and on the home page, you will see how we can connect to Mongoose
with two lines of code as compared to MongoDB.
We wrote at least many lines to get it done, so let's see how we are going to do this one inside our
application.
So first step is we need to require the package core mongoose equal to require the mongoose package.
And here we go.
The next step is let's go ahead and connect to Mongoose.
So here we go.
Connect.
To Mongoose.
All right.
I forgot to bring one over here.
So here we go.
On Mongoose, we have a method called Connect.
And that is it, guys.
And that is it.
So here we pass in our connection string.
Make sure you have your connections trained.
So I'll copy my connection string here and let me come back to the project.
And here I'll provide it.
I'll start.
And remember, it says that Mongoose David was asynchronous environment.
So here it means that we need to handle both success and failure.
It means when everything goes on where we want to tell the user that the connection was successful.
For this one, I'm using promise.
So here I'm going to console.log and it says that DB.
Connect it.
As that.
So let me remove this.
Column here.
And if something goes wrong, I'll bring my catch.
And in here I have the error and I can go ahead and console.log.
The.
Error dot message and we have a message on that and that is it.
You see, I'm done with a connection as compared to MongoDB.
You can see how we're able to connect using this function.
So now how can we text that our connection was successful?
So back to my terminal here.
Make use of node and the file was Mongoose.
So hit enter.
Now let's wait.
And MongoDB has been connected or DB has connected.
So if I try to change the password and let's see if we are going to get some error now let me run the
file again and let's see if you see that I get something called undefined.
So I think there is something wrong or I forgot to bring the E here message.
All right, but on my side, let me run it.
You can see that we got bad of many bad authentication.
That is, authentication failed.
Meaning that I provided wrong password.
So that is it.
So this is how we can connect to MongoDB using Mongoose in the easiest way.
Now that we are done with connection in this video.
Let's see how we can create record and save it into MongoDB.