Mongoose Flashcards
Terminology
Collection
Grouping of MongoDB documents
Equivalent to a Table in SQL
Document
A single { , … }
Which can consist of 1 or many key : value pairs
Intro to Mongoose [1]
Allow node.js app to talk with MongoDB
Allows the user to define the schema for the documents in a particular collection
npm install mongoose
Create reference to mongoose
const mongoose = require(‘mongoose’);
Create new database, or connect to an existing database
Intro to Mongoose [2]
Closing connection
mongoose.connection.close();
Create a mongoose scheme
Intro to Mongoose [3]
Use the scheme to create a Mongoose model
Intro to Mongoose [4]
Adding a new item to the collection
Create a new object based on the model
Then call .save() on that new object
Intro to Mongoose [5]
Saving in bulk
.insertMany(, )
Intro to Mongoose [6]
Reading from your database with Mongoose
.find();
Data Validation with Mongoose
Add validation in the scheme object
Check for the different types of validations in the mongoose documentations
Intro to Mongoose [7]
Updating and Deleting Data with Mongoose
Update
.updateOne(, , Callback);
Delete
.deleteOne(, Callback);
Establishing Relationships and Embedding Documents using Mongoose
MongoDB - NoSQL is great for 1-to-Many relationships
So for establishing relationships, we modify the schema:
In the “1” schema, place in the key:value pair, the “many schema”
Creating Custom Lists using Express Route Parameters
Creating and responding to a dynamic route
Intro to Mongoose [8]
Express Route Parameters
$pull
Removes from an existing array all instances of a value or values that matches a specific condition