Home > Software engineering >  How can I make a query if I search by email then I will get the details of it using node & express
How can I make a query if I search by email then I will get the details of it using node & express

Time:01-30

This is my mongodb database image

enter image description here

I want to make a query if I search by email then I will get the details of it. The email is inside the details. How can I make an API using node & express. This is my code and I am getting an empty array:

        app.get('/dashboard/orders', async (req, res) => {

                const email = req.query.userEmail
                const query = { details: { email: email } }
                const cursor = bicycleOrdersCollection.find(query);
                const result = await cursor.toArray()
                console.log(result);
                res.json(result)


        })

and this is the example of query link http://localhost:5000/dashboard/[email protected]

CodePudding user response:

change your query variable to

const query = {"details.email":email}

this issue you are facing here is that mongo is trying to search for a field with the exact same object you are trying to query, but if you do as above, it will search for a value inside the object and not match the whole object

  •  Tags:  
  • Related