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


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.

  1. Navigate to the project root on the command line.
  2. Create a .env file in the project root and add PORT=3001 (you can change this back to 3000 once you've confirmed dotenv is working).
  3. Install dotenv as a dependency
  4. require('dotenv) in your server

4. Connecting to MongoDB Atlas

Materials

Activity


5. Importing data into MongoDB Atlas

Activity

  1. Assumption: Connected to a MongoDB database in the cloud
  2. 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.
  3. Install Compass
  4. Add your connection string.
    • MongoDB Atlas will create a starter for you, which you will modify with your db login and database name.
  5. Create a Database (call it wbdv-sait or similar)
  6. Create a Collection (plural noun like animals or images)
  7. 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.