Home > Net >  How to filter Data using email in MongoDB?
How to filter Data using email in MongoDB?

Time:05-08

How can I get data by email from MongoDB? Here is an example:

       _id: Objectid('6274e58826914a298567e5f8'),
        "name": "Vauxhall Mokka",
        "supplier": "Vauxhall",
        "email": "[email protected]",
        "price": 30000,
        "quantity": 30,
        "img": "https://i.ibb.co/SQqBNXy/getImage.png",
        "sold": 0

I can easily get the item by _id with this code:

 app.get('/cars/:id', async (req, res) => {
            const id = req.params.id;
            const query = { _id: ObjectId(id) };
            const result = await serviceCollection.findOne(query);
            res.send(result);
        });

But why I couldn't do the same with email? when I apply '/cars/:email', the server goes down. If I get the item with id, then why cannot get it with the email?

CodePudding user response:

No reason why this shouldn't work

 app.get('/cars/:email', async (req, res) => {
   const query = { email: req.params.email };
   const result = await serviceCollection.findOne(query);
   res.send(result);
 });

When the "server goes down", is it giving you an error message?

CodePudding user response:

Your email is missing in node js code you have to grave email from req as like this const email = req.query.email

  • Related