Tutorial on making a database and then using my API-Template from GIT
Tags: javascript, beginners, tutorial, webdev
Published: 2024-02-25 20:18:46

Creating an account and setting up a database with a collection on MongoDB Atlas involves a series of straightforward steps. MongoDB Atlas is a cloud database service that provides you with a hosted MongoDB service, making it easier to set up, operate, and scale your databases. Here's a step-by-step guide you can follow for your tutorial:

Step 1: Sign Up for MongoDB Atlas

Go to the MongoDB Atlas website: Open your web browser and navigate to the MongoDB Atlas website.

https://www.mongodb.com/cloud/atlas/register

Create an account: Click on the "Start Free" or "Sign Up" button. You will need to provide your email, create a password, and fill out any other required information.

Verify your email: After signing up, check your email for a verification message from MongoDB Atlas and follow the instructions to verify your email address.

Step 2: Create a Cluster

Log in to your MongoDB Atlas dashboard: After email verification, log in to your MongoDB Atlas account.


Create a new cluster: Click on the "Build a Cluster" button. You can choose a free tier cluster for testing purposes.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vly2127507iigmh48osf.png)


Step 3: Configure Security Settings
Create a database user: Navigate to the "Database Access" section under "Security". Click "Add New Database User", and create a user with read and write privileges to any database. Remember the username and password, as you will need them to connect to your database.

Whitelist your IP address: In the "Network Access" section, add your current IP address to the IP whitelist to allow connections from your location, 

Click on "Finish and Close"

Now you will be taken to this page 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6nukc2mr5zdaa114ryzg.png)

Click connect


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c3dj9ymjcar55koojejf.png)

Now you will need to click on drivers.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/10m94u73hd6rx4teflnn.png)

You will now need to copy that mongodv+serv string click on the copy icon the right of it. 

Then after that for put that string in a .env file should look like this 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1ue4tjis4fz8x3yuo0tv.png)

For now just go back to atlas and click on the "Add Data "

Then Click on the button "start" Create Database on Atlas
Add data directly in the Atlas interface.

Now Type in the database name Generic and the collection name GenericModel scroll a little bit down and click on "Create Database" button.

Should now be on this screen 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/94zxos8aainn1lh88u7z.png)

Now go back to your VSCode i am just assuming you're using that.
In your .env file need to adjust the string from this 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e26sm39gje9krs6l7e7a.png)

To This 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/boxk3djx4xp4vfsnijqf.png)

Clone the GitHub Repository

Open Terminal or Command Prompt: Access your terminal on macOS/Linux or Command Prompt/Powershell on Windows.

Navigate to your project directory: Use the cd command to navigate to the folder where you want to clone the repository.

cd path/to/your/project/directory
Clone the repository: Execute the following command to clone the repository:

git clone https://github.com/Shawn2208/API-Template.git

Navigate to the cloned directory: Once the cloning process is complete, move into the cloned repository's directory:

cd API-Template

Now just type in the terminal code .env a file should pop up now just put that string inside this file.

You will also need to run 

npm install 

In your terminal installing all the necessary dependencies.

Run: 

node server.js

If you have followed this correctly it will tell you mongodb is connected and the localhost:3000 now the endpoint is:

http://localhost:3000/api/endpoint

It will be blank cause you have no data in your database.

As you proceed to develop this application further, consider exploring more advanced features of MongoDB Atlas such as data encryption, automated backups, and performance monitoring to ensure your application remains robust, secure, and performant.

And also have a look at the README.md file more info is in there.

Give us a like or comment your thought's maybe next i will show you in depth with what needs to be renamed and etc in the API folder.

In conclusion, this tutorial has guided you through the essential steps of setting up a MongoDB Atlas account, creating and configuring a database cluster, and integrating it with an application by cloning a GitHub repository and configuring the connection string within a .env file. From signing up for MongoDB Atlas, securing your database with a user and IP whitelist, to creating a database and collection named Generic and GenericModel, respectively, you have learned the foundational steps necessary to leverage MongoDB Atlas for cloud-based database management.