Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Creating Data with POST | Section
Oefenen
Projecten
Quizzen & Uitdagingen
Quizzen
Uitdagingen
/
Working with MongoDB in Express Applications

bookCreating Data with POST

Veeg om het menu te tonen

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.

question mark

What does user.save() do?

Selecteer het correcte antwoord

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 7

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Sectie 1. Hoofdstuk 7
some-alt