Creating Data with POST
Scorri per mostrare il menu
To store data in MongoDB, you create a new document using a model.
This is usually done in a POST route.
app.post('/users', async (req, res) => {
const user = new User(req.body);
const savedUser = await user.save();
res.json(savedUser);
});
The data comes from req.body. A new instance of the model is created and then saved to the database.
After saving, the stored document is returned as a response.
Example request body:
{ "name": "John", "age": 25 }
This creates a new record in the database.
Tutto è chiaro?
Grazie per i tuoi commenti!
Sezione 1. Capitolo 7
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Sezione 1. Capitolo 7