CPNT 262 Day 19 - Mongoose and MongoDB Atlas
Housekeeping
- Tooltime: Install MongoDB Compass
- Ash and Norv will cover for Tony this afternoon
Trophy of the day
- You have connected to an Atlas database and imported data.
- You will not be able to submit this to Brightspace (otherwise you'd be sharing database credentials)
1. Spoiler Demo: Poisoned endpoint
Modify the List Entry endpoint of the Guild sample code so that it can return a list of poisoned guild members.
2. MVC and NoSQL
Materials
- MVC definition on MDN
- Model-view-controller on Wikipedia
- Takeaways: Getting started with NoSQL
3. dotenv
and environment variables
Materials
Key Takeaways
- Any app that uses Mongoose will have a
.env
file in the root directory that will store your database login information. - IMPORTANT: Add
.env
to your.gitignore
. This will prevent you from pushing private credentials to a public repo.
Activity
Refactor one of your Express apps to use port 3001 using an environment variable.
- Navigate to the project root on the command line.
- Create a
.env
file in the project root and addPORT=3001
(you can change this back to3000
once you've confirmeddotenv
is working). - Install
dotenv
as a dependency require('dotenv)
in your server
4. Connecting to MongoDB Atlas
Materials
Activity
5. Importing data into MongoDB Atlas
Activity
- Assumption: Connected to a MongoDB database in the cloud
- Create a
json
file containing the data you'd like to import (like the local module you're creating for Assignment 5).- Warning: MongoDB (and
mongoose
) assume you'll be importing an array of objects. Any other data types will lead to unexpected results.
- Warning: MongoDB (and
- Install Compass
- Add your connection string.
- MongoDB Atlas will create a starter for you, which you will modify with your db login and database name.
- Create a Database (call it
wbdv-sait
or similar) - Create a Collection (plural noun like
animals
orimages
) - Click "Import Data" and upload your
json
file.
Lab Time
- Create an Atlas database and import your sample data.
- Final Project: choose a group member that will own the database you will use for your project. Follow today's steps and have all group members connect to this database.