Creating Data with POST
Swipe um das Menü anzuzeigen
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.
War alles klar?
Danke für Ihr Feedback!
Abschnitt 1. Kapitel 7
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Abschnitt 1. Kapitel 7