0
0
Expressframework~10 mins

Deleting documents in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Deleting documents
Receive DELETE request
Extract document ID
Call database delete method
Check if document exists
Delete doc
Send success response
End request
This flow shows how an Express app handles a DELETE request to remove a document by ID, checks if it exists, deletes it if found, and sends the right response.
Execution Sample
Express
app.delete('/items/:id', async (req, res) => {
  const id = req.params.id;
  const result = await db.collection('items').deleteOne({ _id: new ObjectId(id) });
  if (result.deletedCount === 0) {
    return res.status(404).send('Not found');
  }
  res.send('Deleted');
});
This code deletes an item by ID from the database and sends a success or 404 response.
Execution Table
StepActionInput/ConditionResultResponse Sent
1Receive DELETE requestURL: /items/123Extract id = '123'No
2Call deleteOne{ _id: new ObjectId('123') }deletedCount = 1No
3Check deletedCountdeletedCount === 0?FalseNo
4Send success responseDeleted documentResponse: 200 OK, 'Deleted'Yes
5Request ends---
6Receive DELETE requestURL: /items/999Extract id = '999'No
7Call deleteOne{ _id: new ObjectId('999') }deletedCount = 0No
8Check deletedCountdeletedCount === 0?TrueNo
9Send 404 responseDocument not foundResponse: 404 Not Found, 'Not found'Yes
10Request ends---
💡 Execution stops after sending response to client (either success or 404).
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
idundefined'123''123''123''123''123'
result.deletedCountundefinedundefined1111
response statusundefinedundefinedundefined200 OK200 OK200 OK
Key Moments - 2 Insights
Why do we check if deletedCount is 0?
Because if deletedCount is 0, it means no document matched the ID, so we send a 404 Not Found response (see execution_table row 8).
What happens if the document exists and is deleted?
The server sends a 200 OK response with message 'Deleted' (see execution_table row 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'id' after Step 1 when deleting '/items/123'?
A'123'
B'999'
Cundefined
Dnull
💡 Hint
Check the 'id' variable in variable_tracker after Step 1.
At which step does the server send a 404 Not Found response for a missing document?
AStep 8
BStep 9
CStep 4
DStep 10
💡 Hint
Look at the 'Response Sent' column in execution_table rows for missing document.
If the document is found and deleted, what is the deletedCount value returned by deleteOne?
A0
Bundefined
C1
D-1
💡 Hint
See 'result.deletedCount' in variable_tracker after Step 2.
Concept Snapshot
Express DELETE route:
app.delete('/items/:id', async (req, res) => {
  const id = req.params.id;
  const result = await db.collection('items').deleteOne({ _id: new ObjectId(id) });
  if (result.deletedCount === 0) return res.status(404).send('Not found');
  res.send('Deleted');
});
Checks if document exists, deletes it, sends 404 if missing.
Full Transcript
This visual execution shows how an Express app deletes documents. When a DELETE request arrives, it extracts the document ID from the URL. Then it calls the database's deleteOne method with that ID. If no document matches (deletedCount is 0), it sends a 404 Not Found response. Otherwise, it sends a success message. The execution table traces two cases: deleting an existing document and trying to delete a missing one. Variables like 'id' and 'deletedCount' update step-by-step. Key moments clarify why we check deletedCount and what responses are sent. The quiz tests understanding of variable values and response steps.