How to Connect MongoDB Atlas Cluster From an Application?
Last Updated :
27 Jan, 2025
To connect your application to MongoDB Atlas, a cloud-based, fully-managed NoSQL database, you'll first need to set up a MongoDB Atlas cluster and configure a few essential settings. MongoDB Atlas offers a secure, scalable platform to manage your database without the hassle of manual maintenance, backups, or server management.
In this article, we will walk you through the steps to create a new MongoDB Atlas cluster, create a new user, whitelist your IP address, and retrieve the connection string to connect your application to the MongoDB Atlas cluster.
What is MongoDB Atlas?
MongoDB Atlas is a fully-managed, cloud-based version of MongoDB, a NoSQL database. It provides a secure, scalable platform for managing MongoDB databases without the need for manual server management, maintenance, or backups. Atlas offers features like automated scaling, high availability, built-in security, and backup options, all while running on major cloud providers such as AWS, Google Cloud, and Microsoft Azure.
Create a MongoDB Atlas Account and Cluster
To begin using MongoDB Atlas, you’ll need to create an account and set up your cluster. Follow these simple steps to get started:
Step 1: Create a New Account
- Visit the MongoDB Atlas website: Go to MongoDB Atlas and click on "Start Free" to begin the registration process.
- Sign up: You can sign up using your email address, Google account, or GitHub account.
- Complete registration: Follow the on-screen prompts to verify your email and complete the registration process.

Step 2: Creating New Cluster
- Log in to MongoDB Atlas: Once your account is set up, log in to your MongoDB Atlas dashboard.
- Create a new cluster: After logging in, you’ll be prompted to create your first cluster. You can choose the free M0 tier to start exploring MongoDB Atlas.
- Configure the cluster: Choose your cloud provider (AWS, Google Cloud, or Azure) and select a region that is geographically closer to your application's location to reduce latency. Name your cluster and click "Create Cluster."

Create a New Database User
After completing registration you will be redirected to the Quick Start page in a Security Section, where you have to create a new database user and Whitelisting your IP Address by which you can access your Database. You can also create a new Database user by following this:
Step 1: Navigate to Create a New User
To create a new user, In Sidebar you will find a Database Access page in a Security section. Open that page and click on ADD NEW DATABASE USER button on the top right hand side to create a new database user.

Step 2: Create a New User and Configure the Role
After that enter a Username and Password. Now, scroll down to Configure the privileges of that User. Scroll down to configure the user’s role. You can choose roles such as Read and Write or Read-Only based on your requirements. Once done, click on the Add User button to create a new user.

How to Whitelist Your IP Address
To allow your application to access the MongoDB Atlas database, it's essential to whitelist your IP address. This process essentially grants permission for specific devices or networks to connect to your database.
Step 1: Navigate to the Network Access Page
In the MongoDB Atlas dashboard, go to the Security section in the left-hand sidebar. Click on the Network Access option, which will take you to the page where you can manage your IP address whitelist settings.

Step 2: Whitelist and Configure IP Address
After that Click on ADD CURRENT IP ADDRESS button to add your current IP Address. It is recommended that you add a 0.0.0.0/0 IP Address by clicking on the ALLOW ACCESS FROM ANYWHERE button so you can access your cluster with any device with your username and password. You can also Add an IP Address for temporarily by enabling that option. After doing that click on the Confirm button to add the IP Address.

Retrieve the Connection String and Choose MongoDB Driver
To connect your Node.js application to MongoDB Atlas, you'll need to retrieve the connection string from the Atlas dashboard. The connection string is essential for establishing a connection between your application and the MongoDB cluster.
Step 1: Navigate to Connect Page
To obtain the connection string and select the appropriate driver, go to the Deployment section in the left-hand sidebar of the MongoDB Atlas dashboard. Click on the Database page under Deployment, and then click the Connect button at the top right. This will take you to the connection options page where you can configure the details for connecting your application to MongoDB Atlas.

Step 2: Choose Driver Option
After that it will show you all the options to access your data through tools and applications. Among those options choose the Drivers. After that select a driver according to your application requirement and follow the given step-by-step instruction to install driver and get a connection URL. It provides Drivers for languages such as Node.js, Java, C, C++, Python, Kotlin, .NET, etc.

Example: Connect MongoDB Cluster with Node.js
In this example, we will use the MongoDB Node.js driver to connect a Node.js application to a MongoDB Atlas cluster. Follow the steps below to set up your application, configure the connection, and interact with the cluster. This guide will help us perform essential operations such as connecting to the database, inserting data, and querying it seamlessly.
Step 1: Get the Connection URL from MongoDB Atlas
To get a Connection string follow the instructions given above, By choosing Node.js Driver the connection string will look like this:
mongodb+srv://<username>:<password>@cluster0.k018jn6.mongodb.net/?retryWrites=true&w=majority
Step 2: Create a new Project and Install Driver
To create a new project, first make sure that Node.js and NPM (It will be included in Node.js) is installed and paths are defined. Now create a new folder and then open the terminal and move to that folder. After doing this type the following command into your terminal:
npm init
After doing this, Install MongoDB Driver by using the following command:
npm install mongodb
Make sure you have properly completed the project setup.
Step 3: Create an index.js File
After project setup is completed, create an index.js file, this is the main file of our application. The below code will automatically create a new database and collection (if not present) and insert a new document (data) inside a collection then it is retrieves the document that we have inserted.
// Import the MongoDB Node.js driver
const { MongoClient } = require('mongodb');
// Replace uri with your actual connection string
//const uri = "mongodb+srv://<username>:<password>@cluster0.k018jn6.mongodb.net/?retryWrites=true&w=majority";
const uri = "YOUR_CONNECTION_URI";
const dbName = 'GeeksforGeeks';
// Function to connect to MongoDB Atlas and perform operations
async function connectToMongoDB() {
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
try {
// Connect to MongoDB Atlas
await client.connect();
console.log('Succesfully Connected to MongoDB Atlas');
// Access the database
const database = client.db(dbName);
// Access a collection
const collection = database.collection('GeeksforGeeks');
// Insert a document
const document = { name: 'Jaimin', age: 20 };
const result = await collection.insertOne(document);
console.log(`Succesfully inserted document into the collection`);
// Query the collection
const query = { name: 'Jaimin' };
const foundDocument = await collection.findOne(query);
console.log('Found document:', foundDocument);
} finally {
// Close the MongoDB Atlas connection
await client.close();
console.log('Connection to MongoDB Atlas closed');
}
}
// Calling connectToMongoDB function
connectToMongoDB();
Step 4: Run Your Application
To run your application, run the following command in your project folder:
node index.js


Conclusion
In conclusion, this article provides a detailed step-by-step guide to connecting your application to MongoDB Atlas. From creating an account, setting up a new cluster, and creating a user to whitelisting your IP address and retrieving the connection string, you now have all the necessary knowledge to get started. Additionally, the example with Node.js demonstrates how to interact with MongoDB Atlas and perform basic database operations like inserting and retrieving documents.
Similar Reads
How To Connect Express Application To MongoDB and MySQL?
Integrating a database like MongoDB or MySQL with your ExpressJS application is very important for efficient data storage and efficient retrieval of data from the storage.MongoDB: Uses the Mongoose or Mongodb Node.js drivers to establish a connection and interact with the database. It's a NoSQL data
7 min read
Connect MongoDB Atlas Cluster to MongoDB Compass
MongoDB Compass is a free GUI for MongoDB. We might want to connect MongoDB Atlas Cluster to MongoDB Compass to take benefit of the GUI model for database administration. By connecting MongoDB Atlas, the fully managed cloud database service, to MongoDB Compass, developers can easily interact with th
6 min read
How To Connect MongoDB Database in a Node.js Applications ?
To connect MongoDB to a Node.js application, you can follow a simple process using the Mongoose library, which provides a flexible way to work with MongoDB. Mongoose acts as an Object Data Modeling (ODM) library, making it easier to structure and interact with MongoDB from Node.js.Prerequisites:Node
2 min read
Creating an Atlas Cluster from a Terraform Template in MongoDB
Introducing automation into database management can greatly enhance efficiency and consistency. MongoDB Atlas, a popular cloud database service, offers a convenient platform for managing MongoDB databases. By using Terraform, an Infrastructure as Code (IaC) tool, users can automate the provisioning
7 min read
Creating and Deploying an Atlas Cluster in MongoDB
MongoDB Atlas is a fully-managed cloud database platform that simplifies deploying, managing, and scaling MongoDB clusters in the cloud. Whether you're a developer or a database administrator, understanding how to set up and deploy a MongoDB Atlas cluster is crucial for leveraging MongoDBâs powerful
6 min read
How to Connect Mongodb Authentication by Node.js?
MongoDB is a popular NoSQL database that provides high performance, high availability, and easy scalability. In many applications, you need to connect to a MongoDB database with authentication to ensure data security. This article will guide you through the process of connecting to a MongoDB databas
3 min read
How to Connect Mongodb Compass to Flask
An effective GUI tool for managing and visualizing MongoDB data is MongoDB Compass. On the other hand, a well-liked Python web framework for creating web apps is called Flask. Integrating your MongoDB data into your Flask web application may benefit from connecting MongoDB Compass to Flask. Through
2 min read
How to Delete a Document From MongoDB Collection using NodeJS?
MongoDB, the most popular NoSQL database, is an open-source document-oriented database. The term âNoSQLâ means ânon-relationalâ. This means that MongoDB isnât based on a table-like relational database structure but provides an altogether different mechanism for data storage and retrieval. This stora
4 min read
How to Run MongoDB as a Docker Container?
MongoDB is an open-source document-oriented database designed to store a large scale of data and allows you to work with that data very efficiently. It is categorized under the NoSQL (Not only SQL) database because the storage and retrieval of data in MongoDB are not in the form of tables. In this
4 min read
Sending Data from a Flask app to MongoDB Database
This article covers how we can configure a MongoDB database with a Flask app and store some data in the database after configuring it. Before directly moving to the configuration phase here is a short overview of all tools and software we will use. MongoDB is an open-source database that stores data
5 min read