Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is Mongoose middleware (pre/post hooks)?
Mongoose middleware are functions that run before (pre) or after (post) certain Mongoose actions like saving or removing data. They let you add extra steps automatically.
Click to reveal answer
beginner
When would you use a pre('save') hook in Mongoose?
Use a pre('save') hook to run code before saving a document, like hashing a password or validating data.
Click to reveal answer
intermediate
What is the difference between pre and post hooks in Mongoose?
Pre hooks run before an action (like saving), so you can modify data or stop the action. Post hooks run after the action, useful for logging or cleanup.
Click to reveal answer
intermediate
How do you define a post hook for the 'remove' action in Mongoose?
You add a post hook by calling schema.post('remove', function(doc) { /* code here */ }); This runs after a document is removed.
Click to reveal answer
intermediate
Can Mongoose middleware hooks be asynchronous? How?
Yes, Mongoose middleware can be async by using async functions or calling next() after async work finishes. This helps wait for tasks like database calls.
Click to reveal answer
Which Mongoose middleware hook runs before saving a document?
Apre('save')
Bpost('save')
Cpre('remove')
Dpost('remove')
✗ Incorrect
The pre('save') hook runs before a document is saved, allowing you to modify or validate data first.
What is a common use case for a post('save') hook?
AHashing a password before saving
BValidating data before save
CPreventing save if invalid
DLogging that a document was saved
✗ Incorrect
Post('save') hooks run after saving, so they are good for logging or triggering actions after data is stored.
How do you make a Mongoose middleware function asynchronous?
AOnly use synchronous code
BUse async keyword or call next() after async work
CReturn a promise without async keyword
DUse setTimeout inside the hook
✗ Incorrect
You can define middleware as async functions or call next() after async tasks to handle asynchronous operations properly.
Which hook would you use to run code after a document is deleted?
Apost('remove')
Bpre('delete')
Cpre('save')
Dpost('save')
✗ Incorrect
The post('remove') hook runs after a document is removed from the database.
Can pre hooks prevent an action from completing?
AYes, but only post hooks can do that
BNo, pre hooks only run after the action
CYes, by throwing an error or not calling next()
DNo, hooks cannot affect actions
✗ Incorrect
Pre hooks run before actions and can stop them by throwing errors or not calling next(), which halts the process.
Explain how Mongoose pre and post middleware hooks work and give an example use case for each.
Think about what you want to do before or after saving or deleting data.
You got /4 concepts.
Describe how to write an asynchronous pre-save hook in Mongoose and why you might need it.
Consider tasks that take time, like talking to other services or encrypting data.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of pre middleware in Mongoose?
easy
A. To connect to the MongoDB database
B. To run code after a database operation completes
C. To define the schema structure
D. To run code before a database operation like save or remove
Solution
Step 1: Understand middleware timing
Pre middleware runs before a database action, allowing preparation or validation.
Step 2: Differentiate pre and post hooks
Post middleware runs after the action, so pre is for before actions.
Final Answer:
To run code before a database operation like save or remove -> Option D
Quick Check:
Pre middleware = before action [OK]
Hint: Pre means before the action starts [OK]
Common Mistakes:
Confusing pre with post middleware
Thinking pre defines schema structure
Assuming pre connects to database
2. Which of the following is the correct syntax to add a pre-save hook in Mongoose?
easy
A. schema.on('save', function(next) { /* code */ next(); });
B. schema.pre('save', function(next) { /* code */ next(); });
C. schema.before('save', function() { /* code */ });
D. schema.post('save', function(next) { /* code */ next(); });
Solution
Step 1: Recall Mongoose middleware method names
Mongoose uses pre and post methods for middleware, not before or on.
Step 2: Check syntax for pre-save hook
The correct syntax is schema.pre('save', function(next) { ... next(); }); to run code before saving.