In the last section we provisioned an instance of Mongo D-B on in lab dotcom we're now going to continue
by installing mongoose on our express API and then instructing mongoose to connect to this database
that we've just provisioned.
So let's get started.
Well first flip on over to our terminal.
I mean to find my running server I'm going to kill it because I want to install a new NPM module.
So this is the one time that we will restart our server even though we're using modem Nordman.
In general I usually like to kill the server whenever I'm stealing an NPM module so I'm going to make
sure I'm inside my server directory and then we will run the command and install dash dash save mongoose
like so.
So what that do it's thing and while that runs we'll flip back over to our code editor and start to
wire up mongoose Nauman side of my index genius file at the very top right underneath the Express require
statement.
We are going to start off by requiring in longus to this file.
So we'll say Konst mongoose equals require longus like so then right above our apt declaration we are
going to instruct mongoose to attempt to connect to that copy of Mongo D-B that we just provisioned.
Now connecting to Mongo divi or any Mongo DB using Mongoose is pretty straightforward.
We're going to call mongoose dot connect and then into this function we're going to pass the address
of the Mongo instance that we've just created.
So to get that instance or to get that address we will flip back over to our left console.
Here it is right here.
I'll make sure that I'm still looking at my e-mail dasht database then going to find the box at the
very top that says to connect using a drive or using the standard Mongo D-B you or I and yep that's
exactly what we want.
So I will take this entire You are Ellar right here if I can highlight it.
There we go.
I will copy it.
I will change back over to my index file and then paste it right in.
Now before we get to comforter with that you might be thinking exactly what I'm thinking.
This is a very special little piece of configuration right here that we probably do not want to accidentally
commit to get up because if we connect or if we commit this get this you or I right here or this address
right here anyone else in the world who has this address can freely connect to our database and make
any changes to it that they want whatsoever.
Now yes this is a development copy of our database and we will use a separate one for production.
But hey there's no good reason to let people mess around with our data.
So rather than pasting that link directly into the connect statement right here we will instead add
that address to our config keys J.S. file.
And remember that's where we are storing all of our different secret configuration data.
So inside of keys yes I will make sure they add a comma after this line and they'll put a new line down
and define lango.
You or I like so now you can do lowercase your I uppercase you or I don't care.
Just make sure you are consistent and then we will paste our link like some OK.
So one last thing inside of your.
If you look carefully at the address you'll notice that it is asking us to enter our D-B username and
our DBI password.
So that is the username and the password that we just created in the last section as a database user.
So that's this right here.
Keep clicking on that by mistake.
So my user name or my database name was Steven and my password was Password.
And so I'm going to take out the user and the brackets around it and replace it with Stephen.
And the same thing for the password as well like the OK that's looking pretty good.
Now the last thing we have to do is change back to our index as File we will require that keys file
into here and that will pass that can that you or I into our next statement.
So we'll do Konst keys equals require config keys like so.
And then to mongoose connect we'll pass keys dot Mongo you or I.
And then please again do triple check make sure the capital our capital I over here it needs to be the
same as whatever you've got over here as well.
So make sure the two are consistent.
Okay.
So that's pretty much it.
That's all we really need to do to get started with mongoose.
Now I'm going to change back over to my terminal.
It looks like mongoose has been successfully installed.
So I will now start my server up by executing a.m. run death.
Now when I do so you may initially see some type of warning on here.
Something it says deprecation warning.
And so I have one that says open.
Blah blah blah.
And then I've also got another one that says DVD prototyped authenticate blah blah blah.
So these two deprecation messages or these two warnings you may or may not see them if you don't see
them.
That's fine.
If you do see them hey that's also fine.
These warning messages are being produced by mongoose automatically.
And so to give you a little bit more insight into what Mangu Mongoose is doing with Mongo Let's pull
up our diagram.
Let's go back to this one over here.
OK.
So remember Mongoose is interacting with Mongo DB Now Mongo itself has a driver layer or a layer of
code that lets outside people interact with all the data inside of it.
The two warnings that we are seeing right now are being produced by how Mongoose is interacting with
Mongo.
So these two deprecation warnings are being produced by the Mongo D-B instance because of some code
that exists inside of mongoose.
So until mongoose fixes their stuff up there is nothing nothing nothing we can do to get these messages
to go away.
So get used to them.
They're going to stick around.
And please just do not worry about them.
It's totally fine to have seen these.
OK.
So in this section we have installed Mungo's.
We wired it up to our application and we're now ready to create a brand new collection inside of our
application and then start saving data to our database.
So let's take a quick break.
We will come back and start to create our first collection so I'll catch you in just a second.