Get Started with MongoDB Atlas — MongoDB
Getting started with MongoDB was a little bit difficult for me but it doesn’t have to be the same story for you. If you are a beginner with MongoDB and want to know how to set up a MongoDB database, I hope this post help you set it up easily.
You are going to use MongoDB to store data and to simplify the configuration, you’ll use a service called MongoDB Atlas.
Creating a MongoDB Atlas account
MongoDB Atlas is a fully-managed cloud database that handles all the complexity of deploying, managing, and healing your deployments on the cloud service provider of your choice (AWS, Azure, and GCP). MongoDB Atlas is the best way to deploy, run, and scale MongoDB in the cloud, which means that they configure and host the database for you. Then, your only responsibility will be to populate your database with what matters: data.
- To sign up for a new MongoDB Atlas account — Click here
- Fill out the registration form with your information and click Sign up or sign up with your Google account.
Create a new cluster
After successful signup:
- Fill in your organization’s name, and project’s name, select JavaScript as your preferred programming language and click the green Continue button. — Skip this step if you already filled this in the signup form.
- Click the Build a database button under Create a database. This should lead you to Deploy a cloud database page.
- On the Deploy a cloud database page, click the Create button. This should be the only free option and should lead you to Create a Shared Cluster page.
- In the Cloud Provider & Region dropdown, leave this as the default (typically AWS).
- In the Cluster Tier dropdown, leave this as the default,
M0 Sandbox (Shared RAM, 512 MB Storage) Free forever
. - For Additional Settings, you can leave it as the default,
MongoDB 5.0, No Backup
.
- In the Cluster Name dropdown, you can give your cluster a name, or leave it as the default,
Cluster0
. - Click the green Create Cluster button at the bottom of the screen. This should lead you to the Security Quickstart page.
You should now see the message M0 Cluster Provisioning... This process will take between 3-5 minutes.
You can wait until the cluster is created before going to the next step.
Create a new user for the database
- On the left side of the Security Quickstart screen, click on Database Access.
- Under Create a Database User, click the green Add New Database User button.
- In the modal, enter a new username and password.
- Under Database User Privileges, leave this as the default option, Read and write to any database.
- Click the Add User button to create your new user.
Allow access from all IP addresses
- On the left side of the screen, click on Network Access.
- Under Add an IP address, click on the green Add IP Address button.
- In the modal, click the ALLOW ACCESS FROM ANYWHERE button. You should see
0.0.0.0/0
in the “Access List Entry” entry field. - Click the green Confirm button.
Connect to your cluster
- On the left side of the screen, click on Database. This should take you to the Database Deployments screen.
- Click the Connect button for your cluster.
- On the Connect to Cluster0 popup modal, click on Connect your application.
You should see the URI you’ll use to connect to your database similar to this: mongodb+srv://<username>:<password>@<cluster-name>.mongodb.net/<db-name>?retryWrites=true&w=majority
.
- Click the Copy icon to copy your URI to your clipboard.
Notice that the <username>
, <cluster-name>
, and <db-name>
fields of URI you copied are already filled out for you. All you need to do is replace the <password>
field with the one you created in the previous step.
And that’s it — you now have the URI to add to your application and connect to your database. The URI should be kept safe in a .env file and imported from there for use.
.env file or dotenv file is a simple text configuration file for controlling your Applications environment constants.
I hope this was helpful.