skip to Main Content

I’m developing a Node.js application using Express and Mongoose to interact with a MongoDB database. I’m trying to implement a search functionality for products, but I’m encountering an issue where my search query is not returning any results, even though I know there are matching documents in the database.
Here’s my current setup:

I have a Product model with fields including ‘title’ and ‘description’.
I’m using a GET route to search products based on a ‘keyword’ query parameter.
My search logic uses a regex to match the keyword in either the title or description fields.

Here’s the relevant part of my code:

`if (req.query.keyword) {
  const query = {};
  query.$or = [
    { title: { $regex: req.query.keyword, $options: "i" } },
    { description: { $regex: req.query.keyword, $options: "i" } },
  ];
  mongooseQuery = mongooseQuery.find(query);
}

const products = await mongooseQuery;`

When I make a request to /api/v1/products?keyword=SanDisk, I get the following console output:

Query: {"keyword":"SanDisk"}
Mongoose Query: {
  "keyword": "SanDisk",     
  "$or": [
    {
      "title": {
        "$regex": "SanDisk",
        "$options": "i"     
      }
    },
    {
      "description": {      
        "$regex": "SanDisk",
        "$options": "i"     
      }
    }
  ]
}
Results: 0

I’ve confirmed that there are products in the database with "SanDisk" in the title or description. However, the search consistently returns 0 results.
I’ve tried several debugging steps, including:

Logging the total number of products in the database
Attempting simpler queries
Checking the structure of sample documents

Despite these efforts, I haven’t been able to identify why the search is not working as expected. Any insights or suggestions on how to troubleshoot this issue would be greatly appreciated.

2

Answers


  1. Chosen as BEST ANSWER

    The issue was resolved by properly handling the keyword parameter in the search method:

    Removed the keyword from this.queryString to prevent interference with other filters. Exclude it too from filtering method This ensured that the keyword search worked independently of other query parameters

      search(modelName) {
       if (this.queryString.keyword) {
       let query = {};
    
       if (modelName === "Product") {
         query.$or = [
           { title: { $regex: this.queryString.keyword, $options: "i" } },
           { description: { $regex: this.queryString.keyword, $options: "i" } },
         ];
       } else {
         query = {
           name: { $regex: this.queryString.keyword, $options: "i" },
         };
       }
       this.mongooseQuery = this.mongooseQuery.find(query);
     }
    
    delete this.queryString.keyword;
    
    return this;
    
    
      filter() {
      const queryStringObj = { ...this.queryString };
      const excludeFields = ["page", "sort", "limit", "fields", "keyword"];
      excludeFields.forEach((field) => delete queryStringObj[field]);
    

  2. Your console output doesn’t match your code.

    It looks like you’re adding the $or to req.query and passing that to mongooseQuery.find():

    if (req.query.keyword) {
      req.query.$or = [
        { title: { $regex: req.query.keyword, $options: "i" } },
        { description: { $regex: req.query.keyword, $options: "i" } },
      ];
      mongooseQuery = mongooseQuery.find(req.query);
    }
    

    Which leaves the keyword property in the query, causing it to yield no results.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search