Q1. How do you implement CREATE operation in Express with database?
For POST route, extract data from
With Mongoose:
With SQL:
req.body, validate, then insert into database.With Mongoose:
const user = new User(req.body); await user.save(); res.status(201).json(user);With SQL:
await db.query('INSERT INTO users SET ?', [req.body]);Q2. How do you implement READ operation?
GET routes to fetch data.
For all items:
For single item with ID:
Handle 404 if not found.
For all items:
await User.find() or SELECT * FROM users.For single item with ID:
await User.findById(req.params.id) or SELECT * FROM users WHERE id = ?.Handle 404 if not found.
Q3. How do you implement UPDATE operation?
PUT or PATCH routes.
Get ID from params, updated data from body.
With Mongoose:
With SQL:
Return updated item.
Get ID from params, updated data from body.
With Mongoose:
await User.findByIdAndUpdate(req.params.id, req.body, { new: true });With SQL:
UPDATE users SET ? WHERE id = ?.Return updated item.
Q4. How do you implement DELETE operation?
DELETE route with ID.
With Mongoose:
With SQL:
Usually return 204 No Content or the deleted item.
Check if item existed first.
With Mongoose:
await User.findByIdAndDelete(req.params.id);With SQL:
DELETE FROM users WHERE id = ?.Usually return 204 No Content or the deleted item.
Check if item existed first.
Q5. What validation should you perform in CRUD operations?
Always validate input: check required fields, data types, formats (email), lengths.
Handle duplicate entries.
Validate that items exist before updating/deleting.
Use libraries like Joi or express-validator.
Never trust client input.
Handle duplicate entries.
Validate that items exist before updating/deleting.
Use libraries like Joi or express-validator.
Never trust client input.
