Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Creating Data with POST | Section
Practicar
Proyectos
Cuestionarios y Retos
Cuestionarios
Retos
/
Working with MongoDB in Express Applications

bookCreating Data with POST

Desliza para mostrar el menú

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?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 7

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Sección 1. Capítulo 7
some-alt