Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to use two select statements with mongo db in node js?

Im in a specific situation where I have to select all records in a mongo db collection but also a record with a specific Id. I understand how to get the whole lot of records but how can I query a record with a specific id as well.

app.get('/:id', (req, res) => {
    Term.find().sort({ "term": 1 })
    .then(result => {
    res.render('about', { title: 'About page', terms: result });
    })
    .catch(err => {
        console.log(err);
    });

    const id = req.params.id;
    Term.findById(id)
    .then(results => {
        res.render('about', { specific: results })
    })
});

would the above code work as I have queried the whole database with Term.find but also Term.findbyId(id)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I think this is what you need, return both things in the same handler.

app.get("/:id", (req, res) => {
  const id = req.params.id;
  Term.find()
    .sort({ term: 1 })
    .then(result =>
      Term.findById(id).then(results =>
        res.render("about", {
          specific: results,
          title: "About page",
          terms: result
        })
      )
    )
    .catch(err => console.log(err));
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading