MongoDB, Mongoose and Atlas hosting
Homework
- Review
- Create, read, update and delete on Wikipedia
- MVC definition on MDN
- Model-view-controller
- Using dotenv package to create environment variables
- Introduction to CRUD and Mongoose
- Read: NoSQL vs SQL Databases
- Read: Mongoose CRUD (Create, Read, Update, Delete)
- Reference: Mongoose Getting Started
- MongoDB Atlas
- Read: Get Started with Atlas
- Read: How to host a RESTful Node.js server with MongoDB Atlas database on Heroku
- Note:
- CORS headers are often not needed with Atlas… but sometimes they are.
- We will be connecting to Mongoose a little differently that what is shown in this article.
- Note:
Morning reflection
Housekeeping
json
enpoint review this afternoon- No Dailies today (we’re only connecting with local code today)
- Goals for the day
- Connect successfully to MongoDB Atlas
- Import your gallery data into Atlas.
1. NoSQL vs SQL
Learning Objectives
- Understand the difference between NoSQL and SQL (relational) databases.
- Define document databases and their terminology.
- Summarize the pros and cons to document databases and MongoDB in general.
Terminology
- Document Database
- A type of NoSQL (Not-only SQL) database that stores its data as documents on the file system. MongoDB is a
json
document database. - Database
- Holds one or more collections.
- Collection
- A collection of documents (i.e. a fancy folder). Collections are analogous to tables in relational databases.
- Document
- MongoDB stores data in the form of
json
Documents (technically BSON documents). Each document is analagous to a row in a relational database.
Materials
Key Takeaways
- MongoDB is a file database engine. It literally stores its data as a folder (i.e. Collection) of
json
files (i.e. Documents). - Working with MongoDB is a little messy so we use the
mongoose
package to:- use a simpler syntax;
- ensure the documents in a collection have the same structure;
- hide the gritty details like promises and asynchronous things.
2. Configuring MongoDB Atlas
Learning Objectives
- Create a MongoDB Atlas account.
- Configure Atlas for outside connections.
- Connect to Atlas with Mongoose.
Materials
Key Takeaways
- 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
winter-2021
or similar. - Confirm you’ve whitelisted all IPs in Atlas.
3. Kitten models
Learning Objectives
- Understand the difference between Mongoose Schemas, Models and Instances.
- Create a Mongoose Schema.
- Compile a Mongoose Model from a Schema.
- Load data into a model from Atlas to create an Instance.
Terminology
- Schema
- A Javascript object that describes/defines the structure of a
json
document. - Model
- A compiled Schema that facilitates communication with a MongdDB server.
- Instance
- A model that contains actual data, either assigned in your script or pulled from a MongoDB database.
Materials
Key Takeaways
- Defining your model is the first step to using
mongoose
(after you have successfully connected).- Example: This animal model describes this animal data.
Open lab-time
Activities
- Importing data into Atlas
-
Converting Javascript objects into
json
filesNote:
json
files are for data only. Don’t include Javascript logic like=
,const
, etc. Stick to arrays and object values.Warning: MongoDB (and
mongoose
) assume you’ll be importing an array of objects. Any other data types will lead to unexpected results.
-
Review Session: json
API endpoints
Dailies
- None today.