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

Issue in using like operator on find for Integer data type in Mongoose

I am having issue in constructing where query in Mongoose for integer data type. The key facevalue is integer data type. When i do find query something like this

Here’s the code

var orCondition = 
[{
    facevalue: {
        '$regex': param,
        $options: 'i'
    }
}]

var data = await Product.find({
    $or: orCondition,
});

I am getting the below given error

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

Cast to number failed for value "i" (type string) at path "facevalue" for model "product"

What is the mistake i am doing on constructing orCondition and how can i fix this?

>Solution :

One option is using $regexMatch:

db.collection.aggregate([
  {
    $match: {
      $expr: {
        $regexMatch: {
           input: {$toString: "$facevalue"},
          regex: ".5"
        }
      }
    }
  }
])

See how it works on the playground example

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