A step by step tutorial on how to setup MongoDB in the cloud with Atlas for free

A step by step tutorial on how to setup MongoDB in the cloud with Atlas for free

This article was originally published at https://www.blog.duomly.com/mongodb-in-the-cloud-with-atlas/ --- ### Intro to setup MongoDB in the cloud with Atlas Almost every application you are trying to build needs a database; depending on the...

This article was originally published at https://www.blog.duomly.com/mongodb-in-the-cloud-with-atlas/


Intro to setup MongoDB in the cloud with Atlas

Almost every application you are trying to build needs a database; depending on the type of data you’d like to keep inside, some developers select SQL databases like PostgreSQL or NoSQL databases like MongoDB.

Nowadays, when MERN and MEAN stacks for applications are more and more popular, many developers select MongoDB. To start a project, you’d need to set up MongoDB locally on your computer or online in the cloud. In my opinion, it’s much easier to set it up online because when the application is ready, you don’t need to move the database and avoid some issues.

That’s why I very often use MongoDB Altas to set up a MongoDB database for the projects I’m creating. It’s easy, and you can start using it for free and later upgrade when necessary.

In this article, I’d like to show you step-by-step how to setup MongoDB in the cloud with Atlas.

As always, if you are the person who prefers to watching than reading, feel free to check out our Youtube channel for the episode.

Let’s start!

What is MongoDB Atlas?

Before I start setting the account and creating a database, I’d like to briefly explain what MongoDB Atlas is and why it’s worth using it.

MongoDB Atlas is a global cloud service provided by MongoDB, and it was created with a thought about developers who’d instead focus on coding than managing databases. We can select between AWS, Microsoft Azure, or Google Cloud Platform as a cloud provider.

MongoDB Atlas can handle and simplify deploying or managing databases and save lots of time on application setup.

The main advantages of MongoDB Atlas are the possibility to select between a world-class cloud partner and an ecosystem that fits our application the most. It also offers built-in security for the data we will keep in the database.

Besides that, MongoDB Atlas also makes it easy to scale a database in any direction, so if the application grows or the number of users decreases, we can adjust the database. There is also well-prepared documentation that will help you to keep the database optimized, well secured, and stable.

Let’s see how to set up the account at MongoDB Atlas and how to create a database for your application.

How to setup MongoDB Atlas?

1. Set up the account on MongoDB Atlas

When entering the landing page of MongoDB you will have to create a new account if you don’t have one yet, and I assume you don’t.

Create an account at MongoDB Atlas

To create an account, press the green button „Start free”. After that, you’ll be taken to the registration form, where you have two options. One is to create an account using Google email, and the other one to pass the data and register traditionally. Also, you can see the information that no credit card is required to start.

After the registration process passes correctly, you’ll get an email, and you will be redirected to the section, which will help you to set up the account.

Name project and organization at MongoDB Atlas

First, you’re going to name the organization, no matter if you are a part of the organization, team, or individual.

Let’s assume that we are building our portfolio, so let’s make Portfolio the name of our organization.

The other field there is a Project Name, and here you can think about different environments when you are working on the bigger project where you’d like to separate the testing env, development env, and production env.

Let’s assume our project is a blog platform, and we are setting the development environment, so let’s call it blog_dev.

Select preferred language at MongoDB Atlas

The next thing that we are going to set up is the language we are using to write our application's backend. This will help you to get useful code snippets to connect with your code, and you can change that later if you change your mind.

For the purpose of the example, let’s select JavaScript and click the „Continue” button on the bottom.

Next, you’ll be taken to the available plans, so you can also select the other options than the free one if you’d like to. We will select the free plan for the purpose of this tutorial.

Select plan at MongoDB Atlas

Let’s select the Create a cluster button on the first tab. Now the account seems to be working fine, and we can create a cluster.

2. Creating a cluster

The application should show you the Create a Starter Cluster screen like the one on the image below, where we can start creating the cluster for our database.

Create Starter Cluster at MongoDB Atlas

So, let’s select the cloud provider and region. We should select the region depending on the location of the server and users.

We have three cloud providers to select from AWS, Microsoft Azure, and Google Cloud. For the purpose of this tutorial, let’s select AWS, but when you are going to try, you can also select other ones if they are more suitable for your project.

As a region, I’d select Frankfurt, as I assume that this part of Europe will be the most suitable for my application.

Below that, there’s another toggle, where you can select a cluster. Here, depending on your needs, you can select what’s more suitable. For the purpose of development and our tutorial, I’d select M0 Sandbox, which is free.

Then there’s an option for additional settings, and it allows us to set up backups for paid clusters. This option can be a great one if you are creating a production project, as doing a backup for your database can sometimes save your life.

And the last option is adding a name for your cluster. There’s a default one, but you can change it to make it more recognizable to you. For the purpose of this tutorial, let’s call our cluster portfolio_blog.

After that, press the Create Cluster button below. Creating the cluster can take a few minutes, and when it’s ready, there are some more steps to do.

Clusters at MongoDB Atlas

3. Create a database user

Now we have to create a user for the database. To do it, let’s press the Database Access on the right sidebar menu.

Database access at MongoDB Atlas

Then, press the Add New Database User button, and you’ll see the modal window with a form. Let’s start by selecting the Authentication method. We can select between three methods, but for the purpose of this tutorial let's select Password as an authentication method.

Next, we have to set up the username and password for the database user. Keep in mind to set it up carefully and not too easy. You can use the option to generate a secure password, which in my opinion, is a good solution.

Below that, you can also set up privileges for the user. To make sure you selected the correct ones, you can read more about them in documentations. I’ll select Read and write to any database privileges for the purpose of this tutorial. Below that, we can also restrict access for the specified cluster to the user we’ve just created, and I’m going to do this because of security reasons.

The last thing you can do is checking the user as a temporary one, which means the user will be deleted after some time.

When it’s ready, you can press the Add User button. Then you will be redirected to the previous dashboard, and you will be able to see a user you’ve created.

4. Add the IP address

The next step in setting our MongoDB is to restrict access to the database and access our cluster only from a particular IP address to avoid security issues.

Set IP to access the database at MongoDB Atlas

Let’s open the Network Access tab on the right sidebar menu and press the green button Add IP Address. Next, you’d see the modal with two fields to fill in. Put the IP address from which you’d like to access the database. For your localhost, it should be your IP, and in the case of the production environment, it should be your server IP. Here you can also mark the entry as temporary if you need it. If that’s ready, press the Confirm button.

5. Load Sample Data

The next step is optional but can be useful; adding sample data can be helpful when you are going to connect the database with the code.

To Load Sample Data, go to the main dashboard, and in the cluster table, press three dots just after the Collections button. When the menu will open, select the Load Sample Data option. Then you’ll see the modal, and you’ll be taken through the process easily.

Loading can take a few minutes, but when it’s ready, you’ll be able to open Collections and see the sample of the data.

Load Sample Data at MongoDB Atlas

When it’s there, you can connect the database with your code.

6. Connect

To connect to your cluster, go to the dashboard, and in the cluster press, the button Connect. Then you will see the modal with two points, one about the IP address and the other one about the user. If they are marked as correct, you can choose the method of connection.

Connecting to the database at MongoDB Atlas

Then you can select the method of connection. You can choose among the three ways, connecting with the mongo shell, connecting with the application through the URL, or using Compass. For the purpose of this tutorial, let’s connect our application with the second option.

Code snippet to connect database at MongoDB Atlas

Here you would be asked about the Node.js version you are using because as you remember, we selected Javascript as our technology. Below that, you can also select if you’d like to see the full driver code or just a string URL.

All you need to pass is the password. And that’s it!

Conclusion

Congratulations! In this tutorial, I showed you how to setup MongoDB in the cloud step by step. We started by creating the account, then creating the cluster, and finally creating the database user. I also showed you how to load some sample data, so you can check that everything works when you’d connect.

We also went through what MongoDB Atlas is and why it’s good to use it. I hope this tutorial was useful. Now you won’t waste any more time setting up a local database to start developing an application, especially using MERN or MEAN stack.

Interactive programming courses

Thank you for reading, Anna from Duomly

database mongodb javascript nodejs

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Seeding a MongoDB Database with NodeJS and Express for the Igbo Dictionary API

I created an ExpressJS endpoint to populate my MongoDB database. This approach was the best for the project since it made the process of creating a newly populated database with just one command. It also made it easier to create temporary databases in testing environments. Seeding a MongoDB Database with NodeJS and Express for the Igbo Dictionary API

MongoDB Database and java applications

Enroll for free demo to acquire the best knowledge on the schema-less database from live industry experts through MongoDB training

How to Use MongoDB as a Real-time database with Node.js

Have you ever run into a scenario where you wanted to update your UI as soon as there is some change to your Mongo database

Node.js REST API with Express & MongoDB - Using a MongoDB Database with Node.js

Node.js REST API with Express & MongoDB - Using a MongoDB Database with Node.js. Learn Node.js framework in simple and easy steps starting from basic to advanced concepts with examples including Introduction, Environment Setup, Node Package Manager, Node Callbacks Concept, Node Buffers Module, Node Streams, Node File System, Node Utility Modules, Node Web Module, Node Express Application, Node RESTFul API, Node Scaling Application.

MongoDB – Database, Collection, and Document

Our MongoDB Online Training provide you to learn about MongoDB strategies with realty. Our MongoDB Online Training also includes live sessions, live Projects, and much