MongoDB & Mongoose Flashcards
What is Mongoose?
Mongoose is an ODM ( Object Data Modeling ) library for MongoDB.
What Does Mongoose Provide?
It provides a higher level of abstraction from MongoDB.
What does an ODM allow us to do?
An ODM allows us to write plain JavaScript to interact with our database.
What are some Mongoose features?
Simple and rapid development, Schema creation, data modeling, and model creation.
How would you create a schema?
You create a schema by creating a variable and storing a new Mongoose schema instance.
const itemSchema = new mongoose.Schema()
What does the schema function accept?
The schema function accepts an object that describes how your document should be structured.
What are some properties our title
can have in our schema?
Our title property can hold, type
which defines what data type this field has to be.
How would you create a model?
First, define a variable for your model. The variable name should start with an uppercase letter.
Now, assign mongoose.model("ModelName", schema)
to this variable. The string represents the model and collection name, while the schema defines the schema you’d like to use for your model.
How can we fetch documents from our database?
To fetch documents in our database, we can use the find
method.
What types of helper functions does Mongoose provide for working with documents?
It provides several helper functions for CRUD operations.
What does a CRUD helper function return?
It returns a mongoose query object.
What does the find
function accept?
It accepts an object that allows us to configure it to find any document we want.
How would you fetch every document in a collection?
To fetch every document, you want to use the find
method and pass nothing inside.
How can find documents with the names of John
and balance greater than or equal to 20?
Model.find( { name : 'John', balance : {
$gte : 20 } } )
What helper function can you use to find an document by id?
You can use the findById
method.
What are the three roles of route parameters in express?
- Route parameters capture dynamic data from the URL, typically inputted by the user or based on other dynamic factors.
- The captured data (e.g., userId) is used within the application to perform specific actions, such as fetching information from a database or rendering specific views based on the captured data.
- In some cases, based on the data captured through route parameters, the application can direct the user to other routes or perform additional logic to handle related operations.
How would you define a parameter in express?
To define a parameter in express, they start with :
.
/:parameter_name
Where are the parameter values stored?
They are stored in req.params
.
With the findById
function, what do we pass in?
We pass in the the document id.
What are queries?
Queries are operations that help us interact with our database.
What are Mongoose queries, and why can we await them?
Mongoose queries are objects used to interact with MongoDB databases. We can await them because they support asynchronous operations, allowing us to wait for database operations to complete without blocking the Node.js event loop.
How can we find a single document based on anything?
In Mongoose, you can find a single document based on any field or combination of fields using the findOne()
.
What method can we use to update a document by using id?
We can use the findByIdAndUpdate
method.
What are other options for findByIdAndUpdate
?
The other option is findOneAndUpdate
.