Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
How to get partially match search data from mongodb in nodejs server api using query?

Actually Im a new mongodb express developer. I want to optimize my backend data fetching.

I have tried many ways but didn't work.

Actually I dont want to pass full name as parameter or search text.

But I'm confused is there any way to build a query that will help me to get partially matched data from mongodb ? like includes("some text") method works.

Here is my code. it works fine but i don't want to filter after fetching:

//Getting products using search query
app.get("/products", async (req, res) => {
    //I want to use this query to optimize data fetching
    let query = {}; //Optimization needed

    //To search products using products name (my current solution)
    if (req.query.name) {
        // query = {name: req.query.name} // Only gives result for exact match
        const cursor = productsCollection.find(query);
        const products = await cursor.toArray();
        const searchText = req.query.name.toLowerCase();
        const filteredProducts = products.filter((product) =>
            product.name.toLowerCase().includes(searchText)
        );
        res.send(filteredProducts);
    }
    //To get all products
    else {
        const cursor = productsCollection.find(query);
        const products = await cursor.toArray();
        res.send(products);
    }
});
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!