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 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.


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://"
MongoClient.connect(uri, function(err, client) {
   if(err) {
        console.log('Error occurred while connecting to MongoDB Atlas...\n',err);
   const collection = client.db("test").collection("devices");
   // perform actions on the collection object

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.


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!

9 thoughts on “Getting Started With MongoDB Atlas with Node.js”

  1. Hello! I follow the steps but I cant connect. When I run node app.js, I get app.js:2, import React from ‘react’; , Syntax error unexpected identifier. And I cant continue with my app because I cant connect with mongodb. Any help please??

  2. Digvijay singh thakur

    Hello there, how can we connect mongoDB atlas with NodeJs hosted on Heroku,I am facing difficulty .Please let me know.

  3. Hi there, I am trying to integrate it in node and after integrating I am getting below error, can some one please help me.

    (node:7188) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.

  4. Glenn, pass additional argument for the NewUrlParser:
    mongoose.connect(”, { useNewUrlParser: true })

    However, if you are using mongoose module in node, I believe there is an issue using this to connect to atlas at the moment, (due to the mongodb+srv in uri for atlas connection), and you’ll just have to deal with that depreciation warning in the meantime to use mongoose.

  5. const collection = client.db(“test”).collection(“devices”);

    TypeError: Cannot read property ‘db’ of null

  6. Excellent blog! Thanks for sharing Information on MongoDB. I really appreciate the blog for such an Wonderful Information.

  7. Did you pass the “client” argument in the callback function of MongoClient.connect?

Comments are closed.