How to use MongoDB Atlas?
Posted 4 years ago by Ryan Dhungel
Category: Mongo DB Web Development Node
Viewed 19480 times
Estimated read time: 5 minutes
Signup with MongoDB Atlas to use MongoDB
Recently mLab has been closed for new signups. But that's not a problem, you can use MongoDB Atlas instead.
This article will guide you on how to get started with MongoDB Atlas.
Visit MongoDB Atlas

Click on Start Free

Complete the form with your email, name, password, accept terms of services and hit the button that says Get started free

Then you will be taken to a new page. There you will see a button that says Build my first cluster

Towards the end of the page, you will see Cluster Name row. Click on that to enter the name for your new cluster/database.

Give a name for your cluster and hit the button that says Create Cluster

Then you will see the following page

Towards the left sidebar, you will see the name of your Cluster along with few options such as CONNECT, METRICS, COLLECTIONS.
White List IP in Mongo Atlas
First click on the left sidebar Network Access under Security
Then you will see the following window popup. Here enter 0.0.0.0/0 as whitelist entry. This will allow use to use our mongo atlas cluster from any IP address. Once done, click Confirm.

Create Database User with Name and Password
You need to create database user to be able to use this service. This is not your login user and password but a different one. It will be used to allow access to the particular mongo cluster that you have just created.
Click on Database Access under Security in the left sidebar and on the following popup window, enter user name and password.

This username and password is required later so you need to remember.
Get Connection URI String
Now its time to finally get the connection URI string that will allow our app to use mongo atlas as a database service in the cloud.
Click on Clusters on the left sidebar under Atlas on top. You will see a popup window. Click on the option that say Connect your application

Pick the second option, Connect Your Application
Get a connection string and view driver connection examples

Then you will see the following window

Click to copy the Connection String.

You will need to replace the part with the actual password you used earlier to create a MongoDB User
Go to your project and create a file with the name .env in the root of the project (this step is explained in the previous lecture). nodeapi/.env
and add the connection string that you just copied and paste to MONGO_URI
Update with your actual password. In the following example, you can see my password
kkkkkk9

As you can see, MongoDB Atlas gives us default test
collection inside our cluster nodeAPI.
You can change that to be something else (for example nodeapi) like so:
MONGO_URI=mongodb+srv://kaloraat_admin:[email protected]/nodeapi?retryWrites=truenodeAPI?retryWrites=true
Then connect your app with database with the following code in app.js
These steps are explained in the previous lecture:
Install dotenv package by running the following codenpm i dotenv
app.js
// import mongoose
const mongoose = require("mongoose");
// import dotenv
const dotenv = require("dotenv");
dotenv.config();
// database connection
mongoose
.connect(process.env.MONGO_URI, { useNewUrlParser: true })
.then(() => console.log("DB Connected"));
mongoose.connection.on("error", err => {
console.log(`DB connection error: ${err.message}`);
});
That's all. Now you can use this connection string to use mongodb in the cloud (mongoDB Atlas)
You can also visually see the data in MongoDB Atlas
Right below NodeAPI (your cluster name). You have the following buttons. Click on COLLECTIONS

That will open up the following window: Click on the Collections tab on the top

You will see the list of databases. I tried creating few users and posts using this database using both test and nodeapi. As a result I have two databases. nodeAPI and test.
You may browse through your collection and update data manually.

Thats all. Now you can start using MongoDB Atlas.
Become a FullStack React Node Developer from Scratch
By the way, If you are looking to become a FullStack React Node Developer, then you might find this Udemy course interesting. This course starts from scratch and takes you on a journey to build a fully functional Social Network App from Scratch to Production. Cheers!!
