Getting Started With MongoDB Atlas with Node.js

MongoDB Atlas is a database as a service platform by MongoDB.

MongoDB deployment and management is one of the pain points for the independent developers and small scale startups who cannot afford MongoDB infrastructure engineer.

MongoDB Atlas solves those issues and now you can use it as a service. In this article, we will cover the MongoDB Atlas account creation and connect to the database using code.

Let’s get started.

MongoDB Atlas Account

First, you need to create the MongoDB Atlas account. Just click on this link to go to the signup page. It’s free and no credit card required.

MongoDB Atlas

Done with the account? Let’s create your first cluster.

MongoDB Atlas Cluster

Once you have created your account, you can now create a cluster. Cluster, as the name suggests, is a set of MongoDB server which you can use to connect with your application.

Click on the green button named “Build New Cluster” placed at the right side of the page.

This will open up a popup which looks similar to this.

MongoDB Atlas Cluster

As of now, MongoDB Atlas provides three types of the cluster:

  • Shared Cluster – Free
  • Dedicated Cluster – Paid
  • Production Cluster – Paid

For test purpose, go with Shared cluster which is free.

You can choose the cloud provider of your choice from Amazon, Microsoft, and Google.

Once it is created, you can see the cluster details on the dashboard.

MongoDB Atlas Cluster Dashboard

Whitelist your IP

Now, switch to the security tab and click on IP Whitelist. You need to add the IP of your machine to allow the connection or you can add 0.0.0.0 to connect from anywhere ( not recommended).

MongoDB Atlas will automatically detect your IP and you can add multiple IP addresses of your choice.

MongoDB Atlas IP Whitelisting

Now, let’s connect with the MongoDB Atlas using Node.

Connecting to MongoDB Atlas Using Node

First, copy the connection string. To obtain connection string, click on the connect button and it will open the pop up like this.

MongoDB Atlas Connection

Click on Connect your application and choose the driver as 3.06. This will provide you the connection string similar to shown below.

mongodb+srv://shahid:<PASSWORD>@cluster0-1q7ty.mongodb.net/test

Copy it and save it to use later. Replace the PASSWORD with the password you have provided while creating the connection.

Let’s do the code.

Create a new folder and initiate the Node project using the following command.

npm init --y

Now, install the MongoDB driver using the following command.

npm i --S mongodb

Now, copy paste the following code.

const MongoClient = require('mongodb').MongoClient;

// replace the uri string with your connection string.
const uri = "mongodb+srv://shahid:<PASSWORD>@cluster0-1q7ty.mongodb.net/test"
MongoClient.connect(uri, function(err, client) {
   if(err) {
        console.log('Error occurred while connecting to MongoDB Atlas...\n',err);
   }
   console.log('Connected...');
   const collection = client.db("test").collection("devices");
   // perform actions on the collection object
   client.close();
});

Save it and run the code using the following command.

node app.js

You should see the connected console in the terminal. Similar to this.

You are successfully connected with your MongoDB Atlas cluster.

Conclusion

Infrastructure management is the key thing when going live in production. There are various factors that involves while managing the infrastructure. MongoDB as a service will take those management pain away and let developers like you focus on the code that matters.

You can visit our MongoDB tutorials here. Replace the connection query string with MongoDB atlas one and everything should work just fine!

Shahid
Shahid

Founder of Codeforgeek. Technologist. Published Author. Engineer. Content Creator. Teaching Everything I learn!

Articles: 126