MongoDB Setup Guide-F24
MongoDB Setup Guide-F24
Introduction
MongoDB, similar to Postgres and most of the other popular database systems, follows a client-server
architecture. For Postgres, the database server resides locally on our laptop (we installed it in Prep Work
1), and the client program we use to access that server is pgAdmin4. Unlike this 100% local setup of
Postgres, for MongoDB, we will setup a cloud-based database server called MongoDB Atlas, and to
access that database server, we will install a client program named MongoDB Compass on our laptop.
Then, we will load some sample data into our database for our Assignment 9. Lastly, we will show you
how to construct an aggregation pipeline query with MongoDB Compass and what to capture for your
Assignment 9 submission.
1
The next page shows three tiers of services. Choose the FREE option shown below and click “Create”:
Under Quick setup, de-select “Preload sample dataset” (we will use our own datasets). Keep “Automate
security setup” selected.
Keep the rest of the default configurations unchanged and click “Create Deployment”.
The next page prompts you to create a database user. Be sure to choose a username and a password
that you can remember. This will be used later for establishing connections to the server (hereinafter
we will call them “connection username and password”). Click “Copy” (which copies the password to
your clipboard), then click “Create Database User” button.
You should see the message “A database user has been added to this project”. Click the “Choose a
connection method” button.
The “Connect to Cluster0” dialog box should now show. Choose the option “Compass”.
2
In the following dialog box, keep the “I don’t have MongoDB Compass installed” option. Copy the
“connection string” (circled in red below) to a text editor so you can use it later when we configure the
connection from MongoDB Compass.
Now we need to download the installation file for MongoDB Compass. In the dialog box above, select
your operating system (“Windows 64-bit” for Windows users, “macOS 64-bit” for Apple users with Intel
chip, and “macOS arm64 (M1)” for Apple users with Apple chip), and click “Download Compass
(1.44.6)” (the latest version number may be different from my screenshot which is fine) to start
downloading the MongoDB Compass installation file to your laptop (we will install it later).
Click Done. You should now see the Overview screen of MongoDB Atlas, with the following navigation
panel on the left-hand side of the screen:
3
Under Security, click Network Access. Click the green “ADD IP ADDRESS” button on the right-hand side.
The following dialog box will be shown. Click “Allow Access from Anywhere”, and “0.0.0.0/0” will
automatically appear in “Access List Entry”. Click “Confirm” and wait for the status of newly inserted
entry “0.0.0.0/0” to become active before you proceed.
4
• Windows users: do not double-click the .exe file, but instead right-click the file and choose “Run
as administrator”.
Launch the MongoDB Compass application you just installed.
• Mac users: do a spotlight search for MongoDB Compass and launch it.
• Windows users: the application should launch automatically once the installation is over. If not,
launch it from the Start menu.
In the MongoDB Compass interface, click Add new connection. In the “New Connection” dialog box,
paste the “connection string” you copied in Part I into the URI box as shown below. A default name will
be automatically generated for this connection. Rename the connection as “90838-mongo”. Also select
the “Favorite this connection” checkbox for future ease of access.
Click the “Save and Connect” button. You will see the connection showing on the left-side panel of your
MongoDB Compass interface.
5
Part III: Creating collections and loading data
In this part, we will create the two collections used in class demonstration and Assignment 9: dogs and
students (a “collection” in MongoDB is conceptually similar to a “table” in a relational database). But
before that, we will first create a database called “90838” that will hold those two collections.
On the left-side panel of your MongoDB Compass interface, mouse-over the “90838-mongo”
connection you just created, and click the “+” icon (circled in red below) that appears on the right side.
In the dialog box below, enter “90838” as the Database Name. It also prompts us for the name of the
first collection we’d like to create under this new database. Enter “dogs” as the Collection Name. Click
“Create Database” to continue.
You should see a new database “90838” appearing on the left-side panel and a new collection “dogs”
underneath it. Under the Documents tab of the dogs collection on the right-side panel, click the green
“+” button (circled in red below) and then “Import JSON or CSV File”.
6
The source file of the dogs data is a CSV file called dogs.csv which you need to download from Canvas.
In the file selection dialog box, navigate to the directory where you store dogs.csv you downloaded and
select that file. In the data import dialog box below, make sure everything is configured the same as
what’s shown below. In particular, you need to manually set the type of the “weight” fields to
“Number”. The type of the other fields should remain as “String”. Click the “IMPORT” button to
proceed.
7
In the “Create Collection” dialog box, enter “students” as the Collection Name, and click the “Create
Collection” button.
Now the Documents tab of the students collection will show on the right-side panel which currently
contains no documents. Click “ADD DATA” and then “Import JSON or CSV File”.
The source file of the students data is a JSON file called r_10000.json which you need to download from
Canvas. In the file selection dialog box, navigate to the directory where you store r_10000.json you
downloaded and select that file. In the data import dialog box below, click the “IMPORT” button to
proceed.
You should see 10,000 documents being loaded into students collection.
You have now completed the setup for MongoDB. You may close MongoDB Compass.