How to use MongoDB Atlas?


Posted 7 months ago by Ryan Dhungel

Category: Mongo DB Web Development Node

Viewed 2690 times

Estimated read time: 5 minutes

Course of the day!

Build Laravel React CRUD/SPA APP in 2 hours

Build Laravel React CRUD/SPA APP in 2 hours

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.

 

Click on CONNECT

 

 

Once you hit CONNECT button. You will see a popup with few options.

 

The first option asks you to Whitelist your connection IP address. Click on Add Your Current IP Address

 

 

Once you hit the Add your current IP Address button, that will auto-populate the IP address.

 

Just click on Add IP Address.

 

Then the second option will ask you to Create a MongoDB User

 

Enter any username and password and hit the button that says Create MongoDB User. This username and password is required later so you need to remember.

 

 

Then you will see the following window. It shows you success message that says: You're ready to connect.

 

Click on Choose a connection method

 

 

Once you hit the button that says Choose a connection method, you will see a new window with 3 options

 

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 code
npm 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!!

 

React Node Social Network from Scratch to Deployment