If you are interested in building Node.js project with MongoDB database, MongoDB provides free unlimited (250 to be exact) amount of database projects that you can host online and access from anywhere.
I personally have been using MongoDB Atlas(Cloud Database Service) for a range of projects like Authentication systems, Messaging apps, E-commerce platforms, Content management systems, and many others.
Today we are going to go over all the steps that are needed to create and connect the MongoDB database to your Node.js project.
Contents
- Step 1: Name Your Project and Create
- Step 2: Create a cluster for your database
- Step 3: Connect to Your Database
- Step 4: Add MongoDB URI to your env file
- Step 5: Connect Node project to MongoDB
First to MongoDB Atlas to create your project.
Step 1: Name Your Project and Create
After you signed up and login into your account you will see a dropdown menu that allow you to create a new project. Click on the dropdown in the left sidebar. Then select “New Project” from the dropdown menu. As you will see in the image below.

Then you’ll be prompted to name your project. Enter a descriptive name, such as “Demo Project”. Click “Next” to proceed.

You can add team members to your project if desired. Enter their email addresses and set appropriate permissions. For personal projects, you can skip this step. Click “Create Project” to finalize your database setup.

Step 2: Create a cluster for your database
Clusters in the MongoDB database are servers that work together to ensure that our data is safe and always available by storing replicas of our databases in different servers so that, even though some servers lose our data, we can easily find the replicas elsewhere.
Now let’s continue with the second step.
After signing up for MongoDB Atlas, you’ll be presented with options to create a new deployment.

To get started with MongoDB Atlas, you should select the Free Tier (M0), which provides 512 MB of storage at no cost. MongoDB Atlas offers various cluster tiers to suit different needs, including the Serverless option for variable workloads and the M10 and above tiers, which are paid options that we won’t be discussing here.

Then you’ll have the option to create a new deployment by choosing your preferred cloud provider (AWS, Google Cloud, or Azure). Next, you’ll need to select a region for your database, with a focus on those marked as “Free tier available.” In this example, we’ve selected AWS as the cloud provider and the Bahrain (me-south-1) region. After that just click on create deployment.

Step 3: Connect to Your Database
Once your cluster is created, click on “Connect” next to your cluster name.

As you can see image below it will take you to add a connection IP address and create a database user. Add your username and password then click on Create Database User.

Next chose the Drivers option.

When you choose the Drivers option, it will take you to the main and final step in the MongoDB Atlas page where you will able to copy the connection string for your database.

Copy this URI and let’s go to our code.
Step 4: Add MongoDB URI to your env file
Now that you have copied the connection sting open up your Node.js project. Go to the .env file in your Node.js project and add the URI. Remember the username and password we created in step 3, add them in the MongoDB connection URI in the place of username and <db_password>
Before
MONGO_URI = mongodb+srv://demo-username:<db_password>@cluster0.mk44f.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0
After
MONGO_URI = mongodb+srv://demo-username:demo-password@cluster0.mk44f.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0
Step 5: Connect Node project to MongoDB
Before we connect we need to install Mongoose. Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node.js. It offers a schema-based approach to structure application data. Mongoose also provides built-in validation at the schema level.
npm install mongoose
Now let’s connect.
The below code sets up a basic Express.js server with MongoDB integration. First, we import necessary modules (Express, dotenv for environment variables, and Mongoose for MongoDB interaction), then create an Express app, configure environment variables, and define a port.
The start
function starts the server by listening on the specified port and connecting to MongoDB using the provided URI. If successful, it logs a message indicating the server has started; otherwise, it logs any errors.
import express from "express";
import dotenv from "dotenv";
import mongoose from "mongoose";
const app = express();
dotenv.config();
const port = process.env.PORT || 8000;
const start = () => {
try {
app.listen(port, () => {
mongoose.connect(process.env.MONGO_URI);
console.log(`SERVER STARTED ON PORT: ${port}...`);
});
} catch (error) {
console.log(error);
}
};
start();
And that’s it, we have created a MongoDB Atlas account, then the database and the cluster for the database, and then we took the connection string from the database and connected our Node.j project with it.
Now you can create and connect Node.js projects with MongoDB for personal or commercial uses. For tutorials like this, check out – frewdevtutorials.com