All right.
And now let's connect to our MongoDB Atlas account.
And before we continue, let me just mention that in between the videos, I switched back to Port 5000.
So remember when we're testing?
I set it up as Port 4000, so I switched back to port 5000.
And also I remove the error from the home run.
So if you don't want to get some weird bugs when you visit the home root, I also suggest removing that
as well.
And before we continue, let me just stress something.
In my opinion, this is somewhat of an advanced course.
So my expectation is already that you do have existing MongoDB Atlas account with proper database access
setup with proper network access setup and you know how to already get the connection string.
Now we'll get it together.
But the idea is that I'm not going to set up the account from scratch.
If you're not familiar with those things, please.
Like I keep mentioning, go back to my Node Xpress project video since I cover all of those steps in
great detail there and I don't want to waste everyone's time and do them one more time.
And in order to get running we'll need a mongo installed because we'll use the connect method.
And in order to do that, we need to grab the command here and then stop the server.
You already know the drill.
So let's stop the server.
Let's clear everything and then we're going to go with NPM install Mongo.
And as I said, I'm actually going to switch this one because I like actually how this one looks better.
So this one is useful if you want to get the commands, but actually prefer this setup as well.
Don't ask me why.
Just a weird preference.
And then we want to create a DB folder.
Now I guess let's start up the application first and then that is going to be in a route again.
So make sure that it's right next to the server we're looking for.
DB And then inside of it.
Let's create our connect JS function meaning file, but the function is going to be over there.
And then inside of it, we want to start by getting the mongoose.
So let's go with mongoose.
So import mongoose.
And that is coming from the mongoose package Mongoose here and it looks like I forgot to add from and
let's set up the function that is going to be looking for the URL.
So that is going to be our connection string.
So let's go here const and then connect ADB and I'll probably mention this 20,000 times, but Mongoose
Connect method returns a promise and therefore in server will set up async await.
So this function will be looking for that connection string.
And then from this function, what do we want to do?
We want to go with a return and mongoose.
So that's our import.
And then we want to go with connect method.
And if you're using Mongoose six, then you don't need to add anymore the deprecation warnings.
So my assumption is that you use Mongoose before and prior to version six you needed to add a bunch
of deprecation warnings.
In this case, you don't have to do that anymore.
You can simply just pass in the URL.
Again, that is going to be our connection string.
And then we want to go with export default and then let's look for connect and DB.
Let's save this.
And now back in the server, here's what I want to do.
I only want to spin up my server If the connection was successful and the way we can do that, we can
set up another function.
So that's going to be our start function, which we invoke all the way somewhere down here.
And let's just start by setting everything up.
And yes, it will have to be a sync because like I just said, connect.
DB Basically Mongoose Connect returns a promise.
So let's go here with can't start.
Now that is equal to my sink.
And then inside of it, we're going to go with tri catch.
That's an awesome start.
And then let's go with a weight.
Again, this is returning a promise we want to go with Connect DB And let's take a look.
And as you can see over here, we're right away have the JS added.
Now, this technically is not a middleware, so I don't want to change this around a little bit where
I want to move it above the middleware and I don't think I'm going to give it a comment yet, maybe
a little bit later.
But I do want to make sure that there's definitely is above the middleware, just like my setup that
way.
And then what is this function looking for?
It's looking for the URL, correct.
Now we don't have the connection string yet, so we will set this one up and I know for sure that the
name will be Mongo URL, at least in my case.
So I'm going to go with the process since I know that I can access my environment variables that way
and then mongo and then underscore URL again, the variable is not there yet.
Then what we want to do is grab this app, that lesson like so and then where we have the catch.
Well, let's just log the error for now.
Let's say here log and then the error.
And essentially the server will only spin up if we're successful, if we can successfully connect to
MongoDB.
Here I want to remove the lesson part and then right after I have the function, I want to invoke it
as well.
Now, of course, the moment there should be big fat error.
Why?
Well, because we don't have the connection string.
So let me try it out.
Let me save it.
And.
Yep.
Notice here we have Mongo connect.
Must be a string.
And actually we got back on the fine.
So what is our next step?
Well, let's take a look at that.
Read me.
So we have the start function now we want to get the connection string, then we want to set up mongo
URL and then we want to provide the credentials and the database name now where we can connect to the
application, where we need to go to our MongoDB Atlas account.
We're looking for connect option.
We're looking for specifically connect your application.
I'm going to go with Node.js 4.0 or later.
And then we want to grab this sucker over here.
So notice that is my user.
John Of course the password is a dummy one at the moment and we have our first database, if that's
what you see.
Beautiful.
Let's move back.
A quick update.
Since I recorded this video, Atlas has changed a few things about the setup.
And even though changes are not big, it can lead to some annoying and hard to troubleshoot bugs.
So let me cover them.
So we're all on the same page.
First, when it comes to database access, we need to add built in role manually.
So let's say I want to add a new user.
And in my case, I'm going to go with Peter.
I'll go with some password here.
That's the one that they generate.
Notice how in here we need to click on add built in row.
If you're not going to select this and if you'll just add the user, you won't be able to connect to
database from your local application.
Even with the correct password and username.
So in here we manually need to click on add built in row and then select roll whether that is an admin
only read.
But in my case, I'm going to go with, read and write to any database, so make sure that it is selected
here.
Now of course if you created the user already before then it's not a big deal.
Then there's not much you need to change.
But if you didn't, if you're creating a new user from scratch, just make sure that you add that built
in URL.
Now the second one is the connect.
So let me go back to the database and when we click on Connect and more specifically, connect your
application.
Now, they don't provide that my first database like I have in the video.
So at the moment we get empty value.
So we just need to manually add that database name, whatever that one is.
That's also something to keep in mind.
In the video, I have my first database in here.
You need to have that value between the forward slash and the question mark.
And lastly, this is really not a change, but I strongly, strongly, strongly suggest using the 000,
meaning that you can access the database from anywhere, not the local IP.
You can technically at your current address while you're developing.
But once we push this up to production, if you use your local IP, it can lead to some unnecessary
and hard to track bugs.
And then in here in that env right after my port, I'm going to go with mongo underscore the url, lets
pass in the connection string here.
And then what I'm looking for here is the password.
So the password that I have, which if I'm being perfectly honest, I think I already forgot and then
the database name.
Now in my case, since I have quite a few applications from the Node Express course, let me take a
look at my collections.
I have the last one here.
Auth So I think I'm going to start with 20 and then hyphen job.
If I in your case, of course you can set up whatever name you want.
So where you have the first database over here, I'm going to go with 20 and then job if I like.
So but of course the name is really up to you, then I'm going to change actually the password.
I'll set it up to my correct password and then once everything's in place again, we want to restart
the server because we're making changes here in the data entry.
So let me add the password and switch back to server address and let me try it one more time to spin
up our server.
Beautiful.
I finally remembered my password, so now I'm back in the server.
Just I will stop the server and let's run and PM start and let's see if everything is correct.
We should see server is listening on port 5000 and now we can move on to the next step.