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
.envfile in the root directory that will store your database login information. - IMPORTANT: Add
.envto 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
.envfile in the project root and addPORT=3001(you can change this back to3000once you've confirmeddotenvis working). - Install
dotenvas 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
jsonfile 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-saitor similar) - Create a Collection (plural noun like
animalsorimages) - Click "Import Data" and upload your
jsonfile.
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.
