We will not be installing MongoDB directly on our machines. Instead, we will be connecting to remote collections using the MongoDB Atlas cloud hosting platform.
-
Create an free account with MongoDB Atlas. You will be prompted to set up your account (the Atlas interface will point you in the right direction):
-
Screen 1: The basics
- Organization: ABC Inc. (or whatever)
- Project Name: Animals (or whatever)
- Preferred Language: JavaScript (obvs)
-
Screen 2: Choose a plan
- Plan: Shared Clusters (Free, duh)
-
Screen 3: Create a Shared Cluster
- Cloud Provider: This doesn't really matter but Tony uses
AWS
. - Region: Tony uses
Oregon
(it's closest).
- Cloud Provider: This doesn't really matter but Tony uses
After you submit, it'll take awhile for the server to set up your Cluster.
When it's done:
Your Express app will need a login to access any databases you set up. You will need your login information later for your connection string.
- From the Atlas Admin panel, click
Database Access
(under "Security" on the left-hand side). Add New Database User
- Authentication Method: Password
- Username: something you'll remember.
- Password: Do NOT use a password you use regularly; it's too easy to accidentally push it to a repo.
- Database User Privileges: Read and write to any database (avoid Atlas admin unless you know you need it)
- Click
Network Access
(under Database Access) Add IP Address
Allow Access from Anywhere
- Caveat: This is inherently insecure but is good enough for class work. Do NOT add any sensitive information to any databases to this cluster; Tony will find it and profit off your hard work.
These are datasets that are more complex than we need for this course but they are a nice resource if you want to dip into heavier demo projects.
The Connection String holds all the information your application needs to connect to your database. This will be stored in your .env
file (along with your PORT
number).
-
Click on
Clusters
(under "Data Storage" in the left-hand menu) -
Under Clusters, click
Connect
. -
Connect your application
-
Copy the
Connection String
(starting withmongodb+srv://
) into an.env
file like so:PORT=3000 MONGODB_URL=mongodb+srv://<username>:<password>@cluster0<some-gibberish>.mongodb.net/myFirstDatabase?retryWrites=true&w=majority
- Your connection string will have real values, except for:
<password>
. Paste your DB password (created in Step 2.2) here.- Change
myFirstDatabase
tomyProject
,favouriteThings
or similar.
- It's important that you use
MONGODB_URL
as a variable name; this is what Heroku uses to connect to Atlas (that's tomorrow).
- Your connection string will have real values, except for:
-
Clone or copy this gist into your workspace.
-
Add your
.env
file (with the connection string you created above) to the root directory.- A sample document,
.env-sample
has been provided to get you started (don't forget to rename it to.env
).
- A sample document,
-
Navigate to your root directory on the command line.
-
Install dependencies:
$ npm install
-
Connect to your database:
$ node server.js
- Success:
Connected to DB...
- Success:
The 'uri' parameter to 'openUri()' must be a string, got "undefined"
- You probably don't have a
.env
file or it's not loading correctly.
- You probably don't have a
- The main error you will run into when connecting is
Authentication Failed
. Try the following:- Confirm you've set the correct username/password in your connection string. You can reset the password in the Atlas control panel.
- Confirm you've changed the database in your connection string to
myProject
or similar. - Confirm you've whitelisted all IPs in Atlas.