0
0
Expressframework~10 mins

Updating documents in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Updating documents
Receive HTTP PUT/PATCH request
Extract document ID and update data
Call database update method
Database finds document by ID
Update document
Send success response with updated document
The server receives an update request, finds the document by ID, updates it if found, and sends back the updated document or an error.
Execution Sample
Express
app.patch('/items/:id', async (req, res) => {
  const id = req.params.id;
  const updates = req.body;
  const updatedItem = await Item.findByIdAndUpdate(id, updates, { new: true });
  if (!updatedItem) return res.status(404).send('Not found');
  res.send(updatedItem);
});
This code updates an item by ID with new data and returns the updated item or a 404 error if not found.
Execution Table
StepActionInputDatabase ResultResponse Sent
1Receive PATCH requestid=123, updates={name:'New'}N/AN/A
2Call findByIdAndUpdateid=123, updates={name:'New'}Document found and updatedN/A
3Check if updatedItem existsupdatedItem != nullTrue404 Not Found if false
4Send updated documentupdatedItem with new nameN/A200 OK with updated document
5EndN/AN/ARequest complete
💡 Execution stops after sending the updated document response or 404 error.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
idundefined123123123123
updatesundefined{name:'New'}{name:'New'}{name:'New'}{name:'New'}
updatedItemundefinedundefined{_id:123, name:'New'}{_id:123, name:'New'}{_id:123, name:'New'}
Key Moments - 2 Insights
Why do we check if updatedItem exists after the update call?
Because if the document with the given ID does not exist, findByIdAndUpdate returns null. Checking updatedItem prevents sending a success response for a non-existent document, as shown in step 3 of the execution_table.
What does the { new: true } option do in findByIdAndUpdate?
It tells the database to return the updated document instead of the original. Without it, updatedItem would be the old document before changes, which can confuse the response as seen in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of updatedItem after Step 2?
Anull
B{_id:123, name:'New'}
Cundefined
DAn error object
💡 Hint
Check the 'Database Result' column in Step 2 of the execution_table.
At which step does the server send the 404 Not Found response if the document is missing?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Check if updatedItem exists' action in Step 3 of the execution_table.
If the { new: true } option is removed, what changes in updatedItem after Step 2?
AupdatedItem will be the original document before update
BupdatedItem will be null
CupdatedItem will be undefined
DupdatedItem will contain an error
💡 Hint
Refer to the key_moments explanation about the { new: true } option.
Concept Snapshot
Updating documents in Express:
- Use PATCH or PUT route with document ID
- Extract ID and update data from request
- Call findByIdAndUpdate(id, updates, { new: true })
- Check if document exists (null means not found)
- Send updated document or 404 error
- { new: true } returns updated document, not original
Full Transcript
This visual execution shows how Express updates documents in a database. First, the server receives a PATCH request with an ID and update data. It calls the database method findByIdAndUpdate with the ID, updates, and the option to return the new document. If the document is found, it updates and returns it. If not found, it sends a 404 error. The variable tracker shows how the ID, updates, and updatedItem change step by step. Key moments clarify why checking for null is important and what the { new: true } option does. The quiz tests understanding of these steps and values.