Challenge - 5 Problems
Express Update Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Express route when updating a document?
Consider this Express route that updates a user document by ID. What will the response be if the user exists and the update is successful?
Express
app.put('/users/:id', async (req, res) => { try { const updatedUser = await User.findByIdAndUpdate(req.params.id, req.body, { new: true }); if (!updatedUser) { return res.status(404).send('User not found'); } res.json(updatedUser); } catch (error) { res.status(500).send('Server error'); } });
Attempts:
2 left
💡 Hint
Look at the option { new: true } in findByIdAndUpdate.
✗ Incorrect
The option { new: true } makes findByIdAndUpdate return the updated document. So the response sends the updated user in JSON.
📝 Syntax
intermediate2:00remaining
Which option correctly updates a document using Mongoose in Express?
You want to update a document by ID and return the updated document. Which code snippet is correct?
Attempts:
2 left
💡 Hint
Check the Mongoose method names and options for returning updated documents.
✗ Incorrect
findByIdAndUpdate with { new: true } returns the updated document. updateOne and update do not return the updated document by default.
🔧 Debug
advanced2:00remaining
Why does this Express update route always return the original document instead of the updated one?
Look at this code snippet. Why does it return the original document before update instead of the updated one?
Express
app.put('/items/:id', async (req, res) => { const item = await Item.findByIdAndUpdate(req.params.id, req.body); res.json(item); });
Attempts:
2 left
💡 Hint
Check the options passed to findByIdAndUpdate.
✗ Incorrect
By default, findByIdAndUpdate returns the document before the update. You must set { new: true } to get the updated document.
❓ state_output
advanced2:00remaining
What is the value of 'result.modifiedCount' after this update operation?
Given this Mongoose update operation, what will be the value of result.modifiedCount if one document was updated successfully?
Express
const result = await User.updateOne({ _id: userId }, { $set: { name: 'Alice' } }); // What is result.modifiedCount?
Attempts:
2 left
💡 Hint
Check the Mongoose updateOne result object properties.
✗ Incorrect
updateOne returns an object with modifiedCount indicating how many documents were modified. If one document updated, modifiedCount is 1.
🧠 Conceptual
expert3:00remaining
Which option best explains why using findOneAndUpdate with { runValidators: true } is important?
Why should you include { runValidators: true } when updating documents with findOneAndUpdate in Mongoose?
Attempts:
2 left
💡 Hint
Think about data integrity and schema rules during updates.
✗ Incorrect
By default, findOneAndUpdate does not run schema validators. Setting runValidators: true enforces validation on the update.