Connect to MongoDB using Mongo atlas to store data

This exercise demonstrates connecting to MongoDB using Mongo Atlas and storing the connection URI in an .env file.

Objective

Connect to Mongo DB using Mongo DB atlas

Description

As mentioned in the previous exercises, we will be using MongoDB as a database to store customers’ information. In the last exercise, we installed mongoose, defined schema, and created a Model. Now we will add a connection to MongoDB. We will use mongoose to establish a connection to MongoDB from our server as it also provides a MongoDB database driver.

There are several ways to configure Mongo DB, such as downloading the installer from their website and installing it locally or using mLab. But for simplification, we will be using MongoDB Atlas.

MongoDB Atlas is a MongoDB Database-as-a-Service platform. It sets up the configuration and hosts the databases

 

Acceptance criteria

  • Create MongoDB atlas account
  • Create a new cluster.
  • Create a new user on the database.
  • Whitelist your IP address.
  • Get the connection URI
  • Store the connection URI in a .env file
  • Connect to your database using mongoose.connect()

 

Hints

The Mongo DB connection URI is in the following format:

mongodb+srv://<username>:<password>@<cluster>.mongodb.net/<dbname>?retryWrites=true&w=majority

 

Install package dotenv for node js to create and use the .env file

npm i dotenv

This helps to store your critical information such as secret tokens, passwords, or database information as environment variables instead of passing them directly as a string.


Connect to your database using the following syntax

mongoose.connect(<MONGO_URI>,  { useNewUrlParser:  true, useUnifiedTopology:  true  });

 

 

Solution

 

MongoDB Atlas is a MongoDB Database-as-a-Service platform. It sets up the configuration and hosts the databases in the cloud thus simplifying the process.

Official documentation

Create MongoDB atlas account

  • Start by going to MongoDB Atlas website. Once you open the MongoDB Atlas page, you should sign up for a new account.
  • Click the Sign In button in the top right corner to open the registration page.
  • Click the Register for a new account link at the bottom of the sign in page.
  • Fill the registration form with your information and press continue.
  • You should now be logged into your new account and see a modal with a green Build my first cluster button, click on it.

 

Create a new cluster:

  • Choose your cloud provider and region, you can leave this as the default provided (typically AWS).
  • Customize your cluster’s specs, you can also leave this as the default provided, M0 Sandbox (Shared RAM, 512 MB Storage) Encrypted.
  • Give your cluster a name, you can also leave this as the default provided, Cluster 0.
  • Now click the green Create Cluster button at the bottom of the screen and verify the image captions they provide.
  • You should now see the message Your cluster is being created – New clusters take between 7-10 minutes to provision. Wait until the cluster is created before going to the next step.

 

Create a new user on the database

  • On the left side of your screen click the Database Access button under Security to start creating a new user.
  • Click the Add New Database User button that is displayed in the next menu.
  • Enter a new username and password. You can set the privileges to read and write to any database if they are not already.
  • Then click Add User to finish creating the user.

 

Add your IP address