Mongoose Middleware Flashcards
What are the types of important Mongoose middleware you should know?
Document, Query and Aggregation Middleware
Mongoose middleware can either be?
pre or post hooks
What is the format for writing Mongoose middleware?
schemaName.pre/post(‘event’, function (next(pre) or val, next(post)){next()});
When does the save event occur?
During the create() and save() Mongoose methods
What are some events in the query middleware?
find/findById, or any Mongoose query function name
During a pre-hook, what does the this keyword refer to for the document, query and aggregation middleware?
document middleware => the object about to be stored, query middleware => the query currently being processed, aggregation middleware => the aggregation object
During a post hook, what keyword is available for each middleware?
document middleware => the the document that has been saved to the database, query middleware => the this keyword referring to the query is still available
How do you run a query middleware for all events starting with a particular word?
/^word/ as the event
How do you add an element at the top/front of an array?
array.unshift(data)
How do you get access to the aggregation pipeline (array of object operations) in the aggregation object?
this._pipeline
Create all three types of middleware for any use case and compare them with what you did in the course.
Do on PC