All right.
So now that we know how our backend is going to function once it's hosted on MongoDB Atlas and
Heroku, then it's time to get started and actually set up our website so that we can deploy our ToDo
list app onto the Internet.
First things first.
Let's go ahead and sign up for a MongoDB Atlas account.
So if you just click on "Get MongoDB" and the first thing that it has here is sign up to the MongoDB
Atlas service. Here where it says "Deploy a free cluster"
you're going to create a new account and you're going to click on "Get started free".
So now we're going to go through a few steps to set up our brand new cluster on MongoDB Atlas.
The first thing we're going to choose is where is our cluster going to be hosted on.
And the cloud provider that we're going to go with is AWS and both AWS and Google Cloud have free
tiers that are available.
We're going to click on AWS and select North Virginia where we can get a free tier for our MongoDB
database without having to enter our credit card details.
Next we're going to scroll down and expand this part where it says "Cluster Tier".
At the moment the option that's selected is M30 which gives you 8GB of RAM and 40 gigabytes of
storage but you're also paying 50 cents an hour. In order to get started
we're going to change our cluster to M) which is the free tier
and you can see now we have a free forever plan and this means that we don't have enter any credit card
details and we can learn without fear of messing up.
So the nice thing about using MongoDB Atlas as opposed to setting up our servers straight up on
AWS is we can get started without having to provide any payment details without any fear of accidentally
messing something up and getting a huge bill from Amazon AWS.
So this is a really really safe environment. And if you find a website getting more and more popular
then you can always scale up at a later stage as well.
Now the next thing is we're going to use our MongoDB 3.6.
Now this is not the latest and greatest version of MongoDB but it will work just fine as it is. Now
for Cluster name
you can rename it if you wish but for this tutorial we're just going to keep it as "Cluster0".
And now we're ready to go ahead and click Create cluster.
And because they do have costs associated with these clusters because they're paying AWS then it's
important for them to check that you are not a robot and you're not trying to take advantage of their
services.
All right, so now your cluster is being created and as they say it takes between 7 to 10 minutes
to provision and they're doing a lot of heavy lifting on the backend setting up everything on AWS.
And so we're going to come back to this a little bit later on.
Now if you go ahead and click on "Security", you can see that we currently have no users.
Let's go ahead and add a brand new user.
So I'm just going to call my user admin-
angela and then I'm just going to enter a password that we're going to use to test with.
So let's call it test123.
Go ahead and enter a user and password and note down those details because we're going to use it very
very soon and then change that uses privileges to be an Atlas
admin
and go ahead and add this user to our list of MongoDB users. These MongoDB users have the credentials
which we'll need to be able to communicate with our database on MongoDB Atlas.
And that's why we set up usernames and passwords to make accessing our database more secure.
Now the next thing we're going to do is we're going to go over to the IP whitelist and we're going
to click on Add an IP address and we're going to click on allow access from anywhere and then go ahead
and click confirm.
Now this again takes a little while for it to set up.
But meanwhile if we head back over to overview then you can see our cluster is now set up and it's ready
to go.
First things first.
Let's go ahead and connect to our cluster.
We click on the connect button here and go ahead and click on connect with the Mongo shell.
So first it checks to see if you already have the Mongo shell installed which we do because we've
been using it for a long time
now. The next thing it says is click on the version of Mongo shell you have to see what URL
you need to connect to it.
Let's go ahead and open up our hyper terminal and we're going to just run "mongo --version" and
this will tell us what is the version of our Mongo shell. And we're on 4.0.2 which
is more than 3.6
so we'll click on this option. And now it gives you a URL to connect to your MongoDB cluster and
you can see that it's got the username baked in and it's also got the URL of a test database in our
cluster.
So let's go ahead and copy this and paste it into our command line.
It doesn't really matter where you do this from.
You can see that at the moment I'm inside the user folder, angelayu, but you can call this command
anywhere you like.
So now let's go ahead and hit enter.
Now once I hit Enter it's going to ask me for a password and the password that it wants as it says here
is it has to be the one for that user that you just set up.
Remember previously when we set up our users under the Security tab. You need to remember what the password
is that you put in here.
And if you don't remember
you can also just go ahead and enter a new password and click it to update the user.
But if you do remember the password,
so previously I set it up with test123, and now it's going to link up to my MongoDB server on Atlas.
And now we no longer see our prompt from the command line
and instead we now have an active session with the MongoDB shell running on the Atlas cluster.
Now here you'll see a warning where it says "shell and server versions do not match"
because if you scroll up and if you remember our shell version is 4.0.2
but remember when we're working with free instances on MongoDB Atlas at least currently anyways
they're limited to 3.6,
so that's why it doesn't match.
But it doesn't affect us at all
so don't worry about that warning. Now that we're inside our Mongo shell and if you remember previously
we had to have another tab that was running mongo for this to work but currently we actually don't
have that at all.
And instead of mongod we're now connected to the cloud based cluster.
And we can start typing in Mongo commands such as "show dbs" and there is an admin and there's a
local db.
Now what if we wanted to see if we were definitely connected to this same cluster?
Well we can go ahead and click on collections under our cluster and we can create a new database.
Let's just call our database test and our collection name also test, hit create.
And now we're going to add a new piece of data to this collection. And to do that we just click on insert
document and we'll call it newData
"Welcome to your database".
All right. Now let's hit insert and put that data into our test collection inside our test database.
And if we go back to our Mongo shell and we say "show dbs" again now you can see we have this new
database called test and if we say "use test" then "show collections" you can see we've got a single collection
called test and then we can say "db.test.find all.
And you can see that new data that you inserted just now.
This is pretty cool.
Now we're able to connect to our cluster using our Mongo shell.
Now the next thing we need to do is to be able to connect to it from our application.
So if you remember, our previous method for running this app was we had to create a new terminal and
we said mongod to start up our MongoDB server on port 27017 in our local system. The next thing
we did was we went back to our previous shell and we used nodemon to run our app.js and our server
started on port 3000.
So now if we head over to localhost:3000 then we can see our ToDo list as it is.
So this is currently running on our local MongoDB database.
Now what if we wanted to swap out a local database?
If we go ahead and hit Control + C and we shut down our MongoDB server on our local host.
So right now if I was to hit "rs" to restart our server, you can see we get a whole bunch of errors.
And it's because we can't actually connect to our Mongo server on 27017.
Well that's because we shut it down.
So how can we instead connect to our MongoDB cluster to save our data in the cloud in Mongo Atlas
instead? If we head back to our cluster and we click on Connect again and we choose the "connect your
application" and then we're going to select the "SRV connection" because we're using the latest version
of MongoDB
and then we're going to copy this address.
So now if we copy that address and we go back into our app.
js, we can now select all of this string other than the last part where we're creating our database
and we can replace it with that string that we just copied.
And this you will notice is on mongodb.net and it's logging in using our admin-angela
username and then we have to provide the password that we created earlier on.
So there's a couple of changes that we need to make to this URL before we can test it.
Now the first thing is you can see that there is a password placeholder here and we have to replace that
with the password that we created for our user.
So we're using the credentials of our admin-angela username and we have to provide the same password
that we used when we created this user.
So we have to replace all of this part including the angle brackets with the password.
And that might be different for you but for me it was Test-123.
This is the initial part.
Now the second thing we have to check is previously we were saying that we were going to create a database
called todolistDB.
But at the moment it's trying to save data into the database that we created earlier on which was called
test.
So what we're going to do is we're going to delete all of this and we're going to get it to save into
the database that we wanted which was todolistDB.
So now if we hit save and we go back into our console and we restart our server, now you can see we're
no longer getting that error right? We're not getting "failed to connect to server" because we're actually
getting a success message.
Now if we go into our browser and we go ahead and go on to localhost:3000 again you can see we now have
a brand new copy of our todo list app with a database that's hosted in the cloud on Mongo atlas.
And if I go ahead and add a new item here
so for example let's say "Test item" and click add,
now not only does it show up in my database, but if I go over to my MongoDB cluster and I click on
"Collections" you can see I now have this new database called todolistDB
and I've got items and lists and inside items
I've got all of my default items plus the one I added just now.
So now we can verify for certain that our database is hosted in the cloud and we're accessing it from our
Node.js app. Now
in the next lesson we're going to set up our app so that we can deploy it on Heroku and get Heroku
to talk to Mongo Atlas and to be able to access our entire website and make it function on the worldwide
web. For all of that and more, I'll see you on the next lesson.