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
.
What does findByIdAndUpdate
take in as arguments?
It takes in a id you want to select from the collection.
The second argument is an object that contains the new data.
The third argument is an options object.
In findByIdAndUpdate
options object, what can you pass in?
You can set runValidators
as true. Which will validate the new data based on the schema.
You can set new
to true. This will return the new document. By default, it returns the old document.
How can you delete a document, or maybe documents?
You can delete a document by using the following functions. findByIdAndDelete
, or deleteMany
.
What does deleteMany
return?
It returns an object with a property of deleteCount
that contains the number of documents deleted.
What does trim do in our schema?
It removed whitespace from the beginning and end of our string.
What does this mean type : [String]
It means, it should hold an array of strings.
What does required mean in our schema?
It means that certain field is required if the property required
is set to true.
Are you able to set default values for your fields?
Yes
Should every field be required? If no, why?
No, because some values will be calculated throughout some period.
What does a query string start with?
URL String
It start with ?
.
What does the query string hold?
It holds data about filtering, sorting, limit, page, and pagination.
How can we chain parameters in our query string?
We can chain parameters with &
.
What does the following example mean? What object should we get from this query string?
?search=javascript
This means, we want to find a document with the search property set to javascript
.
The query object would look like this
{ search : "javascript" }
How can we access the query string?
We can access the query string via our request
object. In this object, we have the query
object.
What is the first thing we need to do when trying to filter our data? Also, why?
The first thing we need to do is create a shallow copy of the req.query
. We do this because we avoid altering the original array.
In our req.query
copy, what do we do first?
What else should we remove after?
We must delete any other parameter that does not deal with filtering.
We should remove any properties that are undefined.