Junior — Middle
How is data deletion from a database performed?
sobes.tech AI
Answer from AI
Deleting data from a database in Node.js is usually done using the appropriate methods of the database driver or ORM.
For example, when working with MongoDB and the Mongoose library, document deletion is done as follows:
// Delete a single document by condition
await Model.deleteOne({ _id: 'someId' });
// Delete multiple documents
await Model.deleteMany({ status: 'inactive' });
If using an SQL database with a library like mysql or pg, deletion is performed via an SQL query:
const id = 1;
await db.query('DELETE FROM users WHERE id = ?', [id]);
Thus, deleting data involves executing a delete command with a condition specifying which records to delete.