Challenge - 5 Problems
Mongoose Middleware Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a pre-save hook modifies a document field?
Consider a Mongoose schema with a
pre('save') hook that changes a field value before saving. What will be the final value stored in the database?Express
const userSchema = new mongoose.Schema({ name: String, active: Boolean });
userSchema.pre('save', function(next) {
this.active = true;
next();
});
const User = mongoose.model('User', userSchema);
const user = new User({ name: 'Alice', active: false });
await user.save();
console.log(user.active);Attempts:
2 left
💡 Hint
The pre-save hook runs before the document is saved to the database and can modify fields.
✗ Incorrect
The pre-save hook sets the
active field to true before saving, so the saved document has active: true. The console logs the updated value.❓ state_output
intermediate2:00remaining
What is the output of a post-remove hook logging the removed document?
Given a Mongoose model with a
post('remove') hook that logs the removed document, what will be printed after calling remove() on a document?Express
const itemSchema = new mongoose.Schema({ name: String });
itemSchema.post('remove', function(doc) {
console.log(doc.name);
});
const Item = mongoose.model('Item', itemSchema);
const item = new Item({ name: 'Book' });
await item.save();
await item.remove();Attempts:
2 left
💡 Hint
The post-remove hook receives the removed document as an argument.
✗ Incorrect
The post-remove hook logs the
name field of the removed document, which is "Book".📝 Syntax
advanced2:00remaining
Which option correctly defines a pre-update hook using async/await?
You want to run code before updating a document using
updateOne. Which code snippet correctly defines an async pre-update hook?Attempts:
2 left
💡 Hint
Async hooks can return a promise or use next callback, but mixing both can cause errors.
✗ Incorrect
Option C correctly uses an async function without next callback. Option C uses await inside a non-async function causing syntax error. Option C mixes async and next callback which is discouraged. Option C calls next without defining it as a parameter causing ReferenceError.
🔧 Debug
advanced2:00remaining
Why does this post-save hook not log the updated document?
A developer writes this post-save hook but it logs the old document state instead of the updated one. Why?
Express
schema.post('save', function(doc) { console.log(doc.modifiedField); }); await Model.findByIdAndUpdate(id, { modifiedField: 'newValue' });
Attempts:
2 left
💡 Hint
Not all update methods trigger save middleware in Mongoose.
✗ Incorrect
Mongoose's
findByIdAndUpdate bypasses save middleware. Only save() triggers pre/post save hooks. To run middleware on updates, use pre/post('findOneAndUpdate') hooks.🧠 Conceptual
expert2:00remaining
How does Mongoose handle errors thrown in async pre hooks?
If an async pre hook throws an error, what is the effect on the document operation?
Attempts:
2 left
💡 Hint
Middleware errors usually stop the operation and propagate the error.
✗ Incorrect
When an async pre hook throws an error, Mongoose aborts the operation and passes the error to the caller, either via callback or promise rejection. This prevents saving or updating invalid data.