Class 11 - MongoDB, Mongoose and Data Modeling
Lab 11 - Can of Books App, Building CRUD apps with MongoDB
Setup
Books are life-changing. They have the power to enlighten, educate, entertain, heal, and help us grow. Throughout this module, you’ll create a small app to track what books have impacted you, and what’s recommended to read next.
Web applications essentially all work by managing data related to “resources”. The resources that an app cares about can be just about anything: a product for sale, an uploaded photo, a review, a bit of weather data… whatever it is that gets stored in a database. When the app provides the interface to create, read, update, and delete a resource, we refer to that as a CRUD app. Over the next few labs, you will build an app that has books as a resource.
For this assignment, you will READ book data by connecting your front-end React app to a back-end Express server. Your Express server will connect to a MongoDB database. You will need to make a “schema” in your back-end code to model how you want your data to look. You will then populate your database with “seed” data—some of your favorite books. When the front end makes a request to your server, your server will query the database and respond with all of the results from the database. Your front end will display these results.
Resources
Yelp API Docs
Process
When working with a partner, take at least 30 minutes to make a team agreement before starting on the assignment. This is an essential step to ensure a peaceful and productive module. You MUST answer the following questions in your agreement and include it in your README’s “Collaboration” section:
Logistical
- What hours will you be available to communicate?
- What platform will you use to communicate (ie. Slack, phone …)?
- How often will you take breaks?
- What is your plan if you start to fall behind?
Cooperative
- Make a list of each parson’s strengths.
- How can you best utilize these strengths in the development of your application?
- In what areas do you each want to develop greater strength?
- Knowing that every person in your team needs to understand the code, how do you plan to approach the day-to-day development?
Conflict Resolution
- What will your team do if one person is pulling all the weight while the other person is not contributing?
- What will your team do if one person is taking over the project and not letting the other member contribute?
- How will you approach each other and the challenge of building an application knowing that it is impossible for two people to be at the exact same place in understanding and skill level?
-
Feature Tasks — READ of CRUD
- Tasks for this lab are tracked in user stories on a Trello board.
- Your instructor will supply you with a link to the Trello board for you to copy (see instructions below).
Workflow
- We will be using the Trello project management tool for the duration of this project.
- After signing in to your account, go to the Can of Books Trello board
Open the “… Show Menu” link, click the “… More” link, and then click “Copy Board”.
- Before you create it, be sure to “Change” from Private to “Public” (and click “Yes, Make Board Public”) so your instructional team can see your work. Now, click “Create” to add a copy to your personal account.
- This Trello board contains all of the features required to complete this lab assignment.
- Review the user stories and analyze the feature requests and requirements in the lab.
Within each story, note the acceptance criteria (“Given … When … Then…”) and the checklist of feature tasks. Be careful to execute tasks in order as they are often dependencies of one another.
- Throughout the lab time, check off tasks as you complete them, and move the story cards through the workflow.
Documentation
Your README.md must include:
Project Name
Author: Your Name Goes Here
Version: 1.0.0 (increment the patch/fix version number if you make more commits past your first submission)
Overview
Getting Started
Architecture
Change Log
Credit and Collaborations
Time Estimates
For each of the lab features, make an estimate of the time it will take you to complete the feature, and record your start and finish times for that feature:
Name of feature: ****____****
Estimate of time needed to complete: _
Start time: _
Finish time: _
Actual time needed to complete: _
Add this information to your README.
Submission Instructions
-
Complete your Feature Tasks for the lab, according to the Trello cards.
- Run your Lighthouse Accessibility report looking for a score of 65 or higher. Make adjustments as needed.
- Create a PR back to the main branch of your repository, showing ALL your work, and merge it cleanly.
- On Canvas, submit a link to your PR. Add a comment in your Canvas assignment which includes the following:
- A link to the deployed version of your latest code.
- A link to your public Trello board.
- A question within the context of this lab assignment.
- An observation about the lab assignment, or related ‘Ah-hah!’ moment.
- How long you spent working on this assignment.
Code Challenge - Includes, Every, Substring, charAt
Overview
Read this overview.
Video
Watch the video for this class from the demo playlist.
Demonstration
Look through these sample problems.
Challenges
Navigate to the javascript folder within your data-structures-and-algorithms repository.
-
Create a new branch for this challenge called strings
git checkout -b strings
-
Retrieve the code challenge from the system
npm run get-challenge 11
-
In your terminal, from the javascript folder, run npm test 11 to execute the tests in this file for this challenge.
-
At this point you will see the failed tests scroll through your terminal window with a brief report of the number of failed tests at the bottom.
-
If you do not see this, verify your installation of Jest by typing npx jest –version in your terminal. Filename typos can make things break!
-
Write code to make the tests pass, one at a time. Let the error messages guide you.
-
Once the test is passing, refactor as needed, then move on to the next challenge.
-
Note, you can also run npm test (without a challenge number) to run all of the tests for every code challenge file assignment during the course all at once. This can get “noisy”, but it’s an opportunity to get a view of your overall progress
Submission
When you have completed the entire set of code challenges and all tests pass, create a pull request from your current branch to the main branch and merge it into main.
You will be able to see a test coverage report in GitHub on the Actions tab of your data-structures-and-algorithms repository. It should match what you saw on your terminal in the above steps. Your graders will be looking at this as well.
Submit a link to your pull request.
Written Class Notes
Compass MongoDB
local host 27017
connect do db, create schema,
in terminal type use pets to switch to database db.dogs.find({hair:’white’})
- connect to db mongo
- create a schema
- mongoose
- api to let us crud in mono
- seed the db
- create a server
-
create an app/react
-
Backend
- env
- MONGODB_URL: mongodb://localhost:27017
- PORT = 3000
- npm install express dotenv cors mongoose
- package json in script start: “node index.js”
- in models/dogs.js
- const mongoose = require(“mongoose”);
- const dogSchema = new mongoose.Schema ({
hair: String,
tail: Boolean,
breed: String,
name: String,
});
- in MONGODB_URL: mongodb://localhost:27017 add /name of db
- seed need to connect to DB and use the schema
- //This dogs ends up being a constructor/class based on that schema
- const Dog = mongoose.model(‘Dog, dogSchema’)
- export dogs.js into seed.js
- node seed.js
- inside compass at the bottom mongosh terminal:
- use pets enter
- db.pets.find();
- db.dogs.find();
- db.dogs.find({hair:”white”})
- db.books.find({title:”Book 1”})
- create server that fetches data from db
- in server.js: get access to dogs schema, to get dogs: app.get(‘./dogs, handleGetDogs), async function handleGetDogs
-
| in index: const require dotenv, const mongoose, server = require (‘./server.js); server.start(process.enb.PORT |
|
3000) |
- seed file brought in Dogs to save and server bring in dogs to get
- use localhost:300/dogs to check
- review:
- index.js is entry point, require: dotenv, mongoose (talks to db), require server.js, connect to db, start server app
- front end
- nom create vite#latest front-end, npm install axios
= env.local: VITE_APP_SERVER http://localhost:3000 or render.com url
- create repo on GH empty
- git init, git remote add origin, git add . git commit,
- ise key={dob._id}
- new components folder with Dogs.jsx, import it in App.jsx
- go to render.com: get compass link, new webservie on render, env var = mongodb_url paste compass link
- get render.com link got to netlifiy, import from GH, key = VITE_SERVER and the link
Read 11 - Readings Overview
Resources Link/Pages
Bookmark and Review
nosql vs sql
- nosql vs sql
sql vs nosql (Video)
- sql vs nosql (Video)
Answer
Statement on why this topic matter as it relates to what I’m studying in this module:
Databases provide a structured way to store and organize data and as your application grows, the amount of data it handles also increases.
nosql vs sql
- Fill in the chart below with five differences between SQL and NoSQL databases:
| SQL |
NoSQL |
| Primarily Relational Databases |
Primarily non-relational or distributed databases |
| Defining and manipulating data |
Queries focused on collections of documents |
| Table-based databases |
Document-based, key-value pairs, graph databases, or wide-column stores |
| Vertically scalable |
Horizontally scalable |
| Predefined schema |
Dynamic schema for unstructured data |
- What kind of data is a good fit for an SQL database?
- Structured and well-defined data, like financial data, customer information, and inventory data.
- Give a real world example.
- Maybe like online stores for storing and managing customer information, including contact details, purchase history, and support interactions in a structured manner.
- What kind of data is a good fit a NoSQL database?
- Unstructured or semi-structured data, hierarchical data, and scenarios requiring horizontal scalability. Like social media data, sensor data, and user-generated content.
- Give a real world example.
- Maybe newspaper outlets for storing and managing diverse content types (articles, images, videos) in a flexible document-oriented format.
- Which type of database is best for hierarchical data storage?
- NoSQL due to their flexible and dynamic schema.
- Which type of database is best for scalability?
- NoSQL for horizontal scalability, where you can add more servers to handle increased load.
sql vs nosql (Video)
- What does SQL stand for?
- Structured Query Language.
- What is a relational database?
- Type of database that organizes data into tables with rows and columns. It establishes relationships between these tables based on common fields.
- What type of structure does a relational database work with?
- Tabular format where data is organized into tables with predefined columns and data types.
- What is a ‘schema’?
- Blueprint or structure that defines the organization of data in a database, including tables, fields, and relationships.
- What is a NoSQL database?
- Type of database management system that does not rely on the traditional relational database model. It is designed to handle unstructured or semi-structured data.
- How does it work?
- For example: MongoDB, a NoSQL database, data is organized into collections rather than tables. Collections contain documents, which are similar to JSON-like objects.
- What is inside of a MongoDB database?
- Collections that store documents. Each document is a set of key-value pairs and may have a different structure from other documents in the same collection.
- Which is more flexible - SQL or MongoDB? and why.
- NoSQL considered more flexible than SQL because it is schema-less. This means that different documents in the same collection can have different structures, allowing for greater flexibility in handling diverse data.
- What is the disadvantage of a NoSQL database?
- It lacks the structured querying capabilities and relationships found in SQL databases. This can make complex queries or data retrieval involving multiple collections more challenging in a NoSQL environment.
Things I want to know more about
- From what I understand of Cache, what if you just set the time stamp to a really long time will that sorta be the same as databases?
Retrospective
Retrospectives are a critical part of Agile, and typically take the form of meetings held by a team at the end of a sprint cycle. To get us acclimated to that process, we will use the format of a retrospectives to guide today’s reflection.
This article gives a nice overview to the role of retrospectives.
- What went well, that I might forget if I don’t write down?
- What did I learn today?
- What should I do differently next time?
- What still puzzles me, or what do I need to learn more about?
- Thinking about each of your assignments for the day, reflect on:
- Is the assignment complete? If not, where exactly did you leave off, and what work remains?
- Do not get bogged down in written analysis; instead, focus on capturing the moment with an eye toward how your observations can guide you toward future productivity.