How to connect MongoDB Atlas to Node.js Application (API)

Part 2

Radhaiya Kulkarni
4 min readAug 10, 2022

In this part we will be connecting MongoDB database to Node.js API, creating user schema

In this series we will be creating a simple Node.js API for User-Authentication firstly creating routes , handling API request then connecting it with the MongoDB Atlas and performing Database operations on MongoDB collections using postman

Before moving forward have a look at part 1 of the series for creating API routes Part 1 :- https://bit.ly/node-API-part_1

Step 1:-Create a MongoDB free cluster

Create an account on MongoDB Atlas , then create a new project after that you will be asked to enter your project name lets keep it Node Project, then asked for Add member (not mandatory, can skip ) .You can refer MongoDB officials docs for creating a cluster

Step 2 :- Creating user and Specify Access

After Creating a free cluster you will be asked to create a username and password we will keep it username-nodeproject ,password-nodeproject, this means user with this credentials will be given read and write permissions to that particular project. Then in access specification click on Add My Current IP Address so that you can access the cluster with your current system or add any other IP if you need

Step 3:- Generating the Database Link

Now after successfully creating our cluster click on Connect , and select Connect your application

You will get a string, copy that connection string

Finally! We made it (just a link)

Step 4 :- Create a database and Connection

Now lets create a database , open your MongoDB cluster and open Collection and choose Add my own data, give any Database name Userdata and collection name Usercollection .Open connection.js file in our Database folder(node-project). Here’s where we will use our mongoose package , we will call a mongoose method known as .connect()and pass the URL to it, in that you need to replace <username> and <password> with the actual values which in our case username-nodeproject , password-nodeproject (step2). import that module in our index.js file . We have also used then catch block to check . Npm start!

connection.js

Step 5 :- Create User Schema and more

In our userSchema.js file we will create a user object and a database collection. Using a mongoose method .Schema(). we will make 3 fields Name , Email and Password and again Export the module but in we use a method called .model(). This method has 2 parameters (important) , First is the string and that string is converted to lowercase made plural and made a collection . Eg if you pass ‘ThisCollection’ it is converted to thiscollections and a collection with this name is created in your database and the second parameter is the object of the model that will assigned to the collection. Import that model in our index.js. As soon as the scripts run open mongo DB atlas and refresh your collections , You will find that a database called as ‘test’ is created and in that a collection called thiscollections is created then where did this test database come from ? and why not the collection is in our Userdatabase which we created. Here’s the answer , remember the connection string we created in that there is a forward slash ‘/’ and a question mark ‘?’ so any string you add in between ‘/?’ that the database is created with it and read / write is performed in that database

userSchema.js
connection.js
index.js

In this part we created a database on MongoDB Atlas established connection with our application and created a User Schema, In next part we will perform read/write task in our database and test our API

Github Repository :- https://github.com/Radhaiya/Node-Project.git

--

--

Radhaiya Kulkarni
Radhaiya Kulkarni

Written by Radhaiya Kulkarni

Backend Developer | Flutter | Web-Dev | UI/UX Designer | GDSC Member

No responses yet