0
0
Expressframework~20 mins

Updating documents in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Update Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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');
  }
});
AResponds with the updated user document in JSON format.
BResponds with a 404 status and 'User not found' message regardless of user existence.
CResponds with a 500 status and 'Server error' message on successful update.
DResponds with the original user document before update.
Attempts:
2 left
💡 Hint
Look at the option { new: true } in findByIdAndUpdate.
📝 Syntax
intermediate
2: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?
AUser.update(id, update, { new: true });
BUser.findByIdAndUpdate(id, update, { new: true }, callback);
CUser.updateOne(id, update, { returnNew: true });
DUser.findByIdAndUpdate(id, update); res.send(updatedUser);
Attempts:
2 left
💡 Hint
Check the Mongoose method names and options for returning updated documents.
🔧 Debug
advanced
2: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);
});
ABecause res.json cannot send updated documents.
BBecause the update data in req.body is ignored by findByIdAndUpdate.
CBecause the route method should be POST, not PUT.
DBecause findByIdAndUpdate defaults to returning the original document unless { new: true } is set.
Attempts:
2 left
💡 Hint
Check the options passed to findByIdAndUpdate.
state_output
advanced
2: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?
A1
B0
Cundefined
DThrows an error
Attempts:
2 left
💡 Hint
Check the Mongoose updateOne result object properties.
🧠 Conceptual
expert
3: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?
AIt automatically creates a new document if none matches the query.
BIt returns the original document instead of the updated one.
CIt ensures the update respects schema validation rules before saving changes.
DIt disables all validation to speed up the update process.
Attempts:
2 left
💡 Hint
Think about data integrity and schema rules during updates.