Search code examples
node.jsreactjsmongodbmongodb-querydata-retrieval

Retrieving data from mongoDB using a field


I want to retrieve all the datas from my mongo db collection "Assets" whose "chapterName" is equal to the string sent through the url, How to get it?

app.get(`/api/assets/get_all/${chapterName}`, (req, res) => {
  Assets.find({}, (err, assets) => {
    if (err) return res.status(400).send(err);
    res.status(200).send(assets);
  });
});

Solution

  • the chapterName will be in params of request, req. let's say chaptername is present in mongoose schema.

    app.get('/api/assets/get_all/:chapterName}', (req, res) => {
      Assets.find({ chaptername: req.params.chapterName }, (err, assets) => {
        if (err) return res.status(400).send(err);
        res.status(200).send(assets);
      });
    });