Instructor: Hello and welcome back.
So, now it's finally time to connect the MongoDB database
that we created with our Express application.
And, the first step in doing that is
to actually get our connection string from Atlas.
So, just like we did before when we connected the database
to Compass and to the Mongo Shell,
we need to get our connection string
in order to connect the application to this hosted database.
Now, in case you're not using the hosted database
on Atlas, then I will also show you
how to connect your local database
with the application a bit later in this video.
Anyway, just click here on Connect Your Application
and then this here is our connection string.
Just make sure that you select Node.js here actually
because the string might change based on that.
Now, we also need the version.
And, of course, it's later than three.
And so, let's now go ahead and copy our connection string
and then back in our application,
we create it as an environment variable
here in our config file.
So, I already have DATABASE_PASSWORD.
Let's create DATABASE now.
And then, put our string in here.
Now, two things about this connection string.
First, here in this place, we need to put in our password.
Now, we could go ahead and copy the password from here
and then paste it here, but I prefer
to leave it in a separate variable
and then replace it in code.
I will just actually change this password here
to an uppercase password.
So, PASSWORD to make it a bit more visible.
Then, second, this here is actually our host,
so basically the place where the database is hosted
and then /test.
Now, this test here is the name of the database
that we want to connect to.
And, in our case, we already created the natours database.
Let's check that out actually here back in Compass.
And, if we go here to COLLECTIONS,
then we see that here, we already have our natours database.
So, we did that by the end of the last section.
You remember?
And so, it's very important that here,
in this connection string, we replace this test
with the name of our database.
So, natours, okay?
So, very important here, again,
do not forget to put this here, otherwise,
it's not gonna work.
Now, in case you're using the local database
like I just mentioned before,
this connection string is a lot easier.
So, let's just create a variable
for that one as well, DATABASE_LOCAL.
But, again, if you're using this database string here,
you don't need to worry about this part.
I'm just doing this in case
that you are using the local one.
And so, in this case, it's quite simple.
It's just mongodb and this is kind of a protocol here.
So, instead of http, Mongo uses these like mongodb
and protocol or something.
Then, localhost and then the port,
which we already talked about a couple of times
and it's 27017, remember that?
And then, just like before, we also need
to specify the name of the database,
which should be natours.
Now, in order for this to work, of course,
you need to keep your MongoDB server
so that Mongo D process running at all times.
So, switching it to the terminal.
So, this local process here is the one that I mean.
So, never close this process, so this terminal window,
when you're working with your local database.
So, otherwise, it's not gonna work.
And, that's it for configuration.
Next up, we need to install a MongoDB driver,
so basically a software that allows our Node code
to access and interact with a MongoDB database.
And, there are a couple of different MongoDB drivers,
but we're gonna use the one
that I would say is the most popular one,
which is called Mongoose, which adds a couple of features
to the more native MongoDB driver.
But, don't worry, in the next lecture,
you're actually gonna learn all about Mongoose
and why we're using it.
For now, let's just go ahead and install it.
So, mongoose, and the version
that I'm gonna use is version five
and so please go ahead and install version five as well
so that we're on the same page with our code here.
So, by the time you're watching this video,
Mongoose six, or even seven maybe,
might already be out there
and then something might have changed
and then your code might not work the exact same way.
So, please just install version five
because that's actually a pretty stable version
at this point and I don't think
that they are gonna add much important features
in the future.
So, in our package.json, it should now be here.
So, 5.5.2, yours is probably a bit later,
but that should not be a problem.
Let's go now to server.js here, which, remember,
is kind of the file where we do all
of the setup of our application.
For example, the environment variables
or the imports or Express application.
And, we also start the server down here.
And so, this is also the file
where we're gonna configure MongoDB.
So, let's start by requiring our mongoose package here.
And so, now we have access to the mongoose variable
and on there, we simply call the connect method.
Now, into this connect method, we need to, of course,
pass in our database connection string.
But, remember that, in there, we still have the password.
So, basically this password placeholder here.
And so, before we pass in the string,
we need to actually replace it.
So, replace it with the real password that we have here.
So, let's quickly do that.
And, I'm simply gonna call this variable DB for database.
And so, let's get our process.env.DATABASE.
So, that is, of course, our variable
where the string here is stored.
So, we're getting that and then replace.
And, we want to replace PASSWORD.
This here comes from the HTML close tag package
that I have on my VS Code.
So, process.env.DATABASE_PASSWORD.
Give it a save.
And, okay, so that makes sense.
So, we're simply replacing this placeholder string
with our real password in this connection string.
And so, now we can use this variable
in our mongoos.connect().
So, in here, the first argument is actually
that connection string, so DB.
And then, second, we pass in an object with some options
and these are just some options that we need to specify
in order to deal with some deprecation warnings.
So, don't worry too much about these ones.
So, useNewUrlParser, set it to true.
Then, we need to use useCreateIndex,
also set to true.
And then, useFindAndModify
and set it to false.
So, again, these here are just some options
to deal with some deprecation warnings.
And so, when you're creating your own applications,
just go ahead and use just exactly the same.
Now, this connect method here is gonna return a promise
and so let's actually handle that promise by using then().
And, this promise here actually gets access
to a connection object.
So, let's simply call it con here for connection.
So, basically this connection will
be the resolved value of the promise.
And so, just to show you that we're actually connected,
I want to just log this object to the console.
So, console.log connection and actually I want
to see the connections property on there.
And, we can also log to the console
something like DB connection successful.
Give it a save here.
And then, of course, it displays these warnings here
because of our ESLint, but don't worry about that.
That's just the way that we configured it.
And so, let's now go ahead
and actually run this application.
Now, let me remember what kind of scripts we have.
So, we have start:dev and start:prod.
Now, since our script here is not just called start,
which is kind of a standard,
we always have to type npm run start:dev
instead of writing just npm start, without the run.
And so, let me go ahead
and change the name here back to start.
And then, we have start for production.
So, npm start.
The app is running now.
And, indeed, our database connection was successful.
So, great.
And now, here we have our connections object
and, as you see, there's a lot of stuff in here
and we can see our username here, host,
the password, of course.
And, this stuff here doesn't really matter, of course.
I just wanted to log it here
just so we see that our connection was actually successful.
And so, let's get rid of that.
And, we can actually get rid of all this here as well.
So, this is just a one-liner here.
Oh yeah, just missing this parenthesis.
Now, here, it's saying me that we declared this variable,
but never used it, so let's, indeed, get rid of it.
Now, just as a final step,
let me show you how we could also connect
to the local database, again,
in case you are using that one.
So, let's just duplicate this line, comment it out.
So, this is the hosted database version
and then here we would just have to put in the local one.
So, that's DATABASE_LOCAL.
Let's give it a save.
And, we're also connected successfully to that one.
So, that's just how you would do it in that case.
But, as I said, in this course,
we will actually use the hosted one,
so that's the one that I'm keeping here.
Actually, I'm deleting this one.
We don't need it anymore.
Now, of course, there could also
be problems connecting to the database.
For example, the host might be down
or we might have some error in our connection string
and, in that case, we should catch that error.
But, we will leave error handling
for a bit later in the course
and so for now, I'm not having any catch method here.
Just the then method here,
assuming that everything works fine.
And, now finally, I want to actually go ahead
and delete the collection
that we already created in our database
just so that our Mongoose driver does not interfere
with that in any way so that there's no problem there.
And so, let's move back to Atlas here.
Hit on our COLLECTIONS.
And then, simply delete the collection from here.
And now, we need to put in here the name of the collection
just so we don't do any mistake here.
So, drop and, all right.
So, now we have an empty database here,
ready to start filling with our data starting right
in the next couple of videos.