Quiz Entry - updated: 2026.06.20
How do you check if an update operation matched any documents?
Inspect the matchedCount property of the object returned by the update to see how many documents the query matched.
const updateResult = await votes.updateOne(
{ email: { $eq: body.email }, token: { $eq: body.token } },
{ $set: { vote: parseInt(body.option) } }
);
if (updateResult.matchedCount === 1) {
// Document was found and updated
} else {
// No document matched the query
res.status(400).json({ message: 'no record found' });
}
This is useful for validating that a record exists before confirming success.