Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Reading Data with GET | Section
Working with MongoDB in Express Applications

bookReading Data with GET

Pyyhkäise näyttääksesi valikon

To retrieve data from MongoDB, you use methods provided by the model.

To get all documents:

app.get('/users', async (req, res) => {
  const users = await User.find();

  res.json(users);
});

To get a single document by id:

app.get('/users/:id', async (req, res) => {
  const user = await User.findById(req.params.id);

  res.json(user);
});

Examples:

  • '/users': returns all users;
  • '/users/123': returns user with id 123.

These methods allow your API to read data from the database and return it to the client.

question mark

What does User.find() do?

Valitse oikea vastaus

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 8

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Osio 1. Luku 8
some-alt