0
0
Expressframework~20 mins

Deleting documents in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Deleting Documents Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when this Express route deletes a document?

Consider this Express route that deletes a user by ID from a MongoDB collection using Mongoose. What will the client receive if the user is successfully deleted?

Express
app.delete('/users/:id', async (req, res) => {
  try {
    const result = await User.findByIdAndDelete(req.params.id);
    if (!result) {
      return res.status(404).send('User not found');
    }
    res.send('User deleted');
  } catch (error) {
    res.status(500).send('Server error');
  }
});
AThe client receives a JSON object with the deleted user data.
BThe client always receives 'User deleted' regardless of user existence.
CThe client receives a 404 error even if the user was deleted.
DThe client receives 'User deleted' if the user existed and was deleted.
Attempts:
2 left
💡 Hint

Look at the condition checking if result is null or not.

📝 Syntax
intermediate
2:00remaining
Which option correctly deletes a document by ID in Express with Mongoose?

Choose the correct syntax to delete a document by ID using Mongoose in an Express route.

Express
app.delete('/items/:id', async (req, res) => {
  // delete code here
});
Aawait Item.findByIdAndRemove(req.params.id); res.send('Deleted');
Bawait Item.deleteById(req.params.id); res.send('Deleted');
Cawait Item.deleteOne({_id: req.params.id}); res.send('Deleted');
Dawait Item.remove({_id: req.params.id}); res.send('Deleted');
Attempts:
2 left
💡 Hint

Check the official Mongoose method names for deleting by ID.

🔧 Debug
advanced
2:00remaining
Why does this delete route always return 'User deleted' even if no user exists?

Look at this Express route code. It always sends 'User deleted' even if the user ID does not exist in the database. What is the bug?

Express
app.delete('/users/:id', async (req, res) => {
  try {
    await User.findByIdAndDelete(req.params.id);
    res.send('User deleted');
  } catch (error) {
    res.status(500).send('Server error');
  }
});
AThe route should use findOneAndDelete instead of findByIdAndDelete.
BThe catch block should send 'User deleted' instead of 'Server error'.
CThe code does not check if the user was found before sending the response.
DThe route is missing async keyword before the callback.
Attempts:
2 left
💡 Hint

Think about what findByIdAndDelete returns when no document is found.

state_output
advanced
2:00remaining
What is the value of deletedCount after this delete operation?

Given this Mongoose delete operation, what will deletedCount be if the document with the given ID exists?

Express
const result = await User.deleteOne({_id: req.params.id});
const deletedCount = result.deletedCount;
Anull
B1
Cundefined
D0
Attempts:
2 left
💡 Hint

Check the Mongoose deleteOne result object properties.

🧠 Conceptual
expert
3:00remaining
Which Express middleware pattern best handles errors during document deletion?

When deleting documents in Express routes, which pattern is best to catch and handle errors globally?

AUse try-catch inside each route and call <code>next(error)</code> on catch to pass errors to error middleware.
BUse only <code>res.status(500).send()</code> inside catch blocks without calling <code>next()</code>.
CWrap all routes in <code>Promise.resolve()</code> to catch errors automatically.
DIgnore errors and rely on Express default error handler.
Attempts:
2 left
💡 Hint

Think about how Express error middleware works with next().