Setting Up MongoDB

You may have experience working with relational databases that are specified using a structured query language (SQL).  These databases consist of  sets of tables. Each table can be conceptualized as having rows and column, where each row is a record in the table and the columns define the types of data that are stored in each record.

A MongoDB database is a NoSQL database which does not contain tables, but rather uses collections of documents.  In MongoDB, a collection is defined using JavaScript object notation (JSON) and each document in the collection is an instance of the JSON object.

In this chapter we will start up a MongoDB Atlas database.   We’ll also install a program named MongoDB Compass which we’ll use to view the contents of the databases.

Create a Production MongoDB Database

Go to the MongoDB Altas homepage at https://www.mongodb.com/cloud/atlas/register.

Click the Start free button.

Register for an account by adding your email address, first name, last name, and passsword. Click on the check box to agree to the terms of service and press Get started free. Click the link in the verification email.

Now, navigate to cloud.mongodb.com and log in.

Caution: Throughout this process of creating a database on Atlas, make sure you are mindful and only select technologies on the Free Tier.

Create a new project. Name it anything you’d like. Click Next, then click Create Project.

On the Database Deployments page, click Build a Database.

On the Deploy a cloud database page, click the Create button in the FREE Shared server card.

On the Create a Shared Cluster page, you only need to change the cluster name to Altas API DB. The other default settings (shown below) should suffice.

Cloud Provider: AWS
Region: N. Virginia
Cluster Tier: M0 Sandbox (Shared RAM, 512 MB Storage)
Additional Setting: MongoDB 4.4, No Backup

When ready, click Create Cluster.

On the Security Quickstart page, for 1) How would you like to authenticate your connection, choose Username and Password, enter a username and password (save your password; you’ll need it later in this tutorial), then press Create User.

For 2) Where would you like to connect from, choose My Local Environment and set the IP address to 0.0.0.0/0 and set the description to anywhere. When complete press Add Entry.  Setting the IP address to 0.0.0.0/0 will allow connections from anywhere.

After the IP address has been entered, press Finish and Close. You will be redirected to the Database Deployments page. Once your database has been provisioned, the button labeled Connect should be enabled.

Retrieve the MongoDB Connect Connection String

On the Database Deployments page, when the Connect button is enabled, press Connect. On the next page that reads Choose a connection method, choose Connect using MongoDB Compass, copy the connection string, and press Close.

Install MongoDB Compass

MongoDB Compass is a GUI application that allows us to view the collections and documents in a MongoDB database.

Navigate to https://www.mongodb.com/try/download/compass and download the appropriate (full) version for your operating system.  Once downloaded, install the app.

Test MongoDB Compass

Open MongoDB Compass and close the opening tutorial.  On the Privacy Settings page, choose your privacy settings, and click Start Using Compass.

Now press New Connection found at the top of the left hand side panel.

Next to New Connection in the right side panel, click FAVORITES. In the pop up dialog, name the connection Atlas API DB, choose a color, and press Save.

Next, paste your Atlas database connection string in the field under the label that reads Paste your connection string. Before pressing connect, replace <password> with the password that you entered when you created your database user account.

When you press Connect, MongoDB should connect to your Atlas MongoDB database.  If you see the hosts displayed on the left and local and admin databases on the right, you have successfully connected to your Atlas database.

 


© 2022 – 2024, Eric McGregor. All rights reserved. Unauthorized use, distribution and/or duplication of this material without express and written permission from the author is strictly prohibited.