Updating Data
Desliza para mostrar el menú
To update existing data in MongoDB, you use methods provided by the model.
A common method is findByIdAndUpdate.
app.put('/users/:id', async (req, res) => {
const updatedUser = await User.findByIdAndUpdate(
req.params.id,
req.body,
{ new: true }
);
res.json(updatedUser);
});
The first argument is the id, the second is the new data, and the third option ensures the updated document is returned.
Example:
PUT '/users/123': updates user with id 123.
This allows your API to modify existing records in the database.
¿Todo estuvo claro?
¡Gracias por tus comentarios!
Sección 1. Capítulo 9
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Sección 1. Capítulo 9