Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Updating Data | Section
Working with MongoDB in Express Applications

bookUpdating Data

Свайпніть щоб показати меню

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.

question mark

What does findByIdAndUpdate do?

Виберіть правильну відповідь

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 9

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Секція 1. Розділ 9
some-alt