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
Mongoose Middleware with Pre and Post Hooks
📖 Scenario: You are building a simple Express app that uses Mongoose to manage a list of books in a library. You want to add middleware hooks to log messages before saving a book and after deleting a book.
🎯 Goal: Create a Mongoose schema for books, add pre-save and post-delete middleware hooks, and export the model for use in your Express app.
📋 What You'll Learn
Create a Mongoose schema named bookSchema with fields title (string) and author (string).
Create a Mongoose model named Book using bookSchema.
Add a pre-save middleware hook on bookSchema that logs "Saving book: [title]" before saving.
Add a post-delete middleware hook on bookSchema that logs "Deleted book: [title]" after deleting.
💡 Why This Matters
🌍 Real World
Middleware hooks in Mongoose are useful to add logging, validation, or other side effects automatically when data changes in a database.
💼 Career
Understanding Mongoose middleware is important for backend developers working with Node.js and MongoDB to maintain clean and maintainable data logic.
Progress0 / 4 steps
1
Create the Book Schema
Create a Mongoose schema called bookSchema with two string fields: title and author.
Express
Hint
Use new mongoose.Schema({ title: String, author: String }) to create the schema.
2
Create the Book Model
Create a Mongoose model called Book using the bookSchema.
Express
Hint
Use mongoose.model('Book', bookSchema) to create the model.
3
Add Pre-save Middleware Hook
Add a pre-save middleware hook on bookSchema that logs "Saving book: [title]" before saving. Use function(next) and this.title inside the hook.
Express
Hint
Use bookSchema.pre('save', function(next) { ... }) and call next() after logging.
4
Add Post-delete Middleware Hook
Add a post middleware hook on bookSchema for the 'deleteOne' event that logs "Deleted book: [title]" after deleting. Use function() and access this.title inside the hook.
Express
Hint
Use bookSchema.post('deleteOne', { document: true, query: false }, function() { ... }) to access the deleted document.
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.