Home > Back-end >  Issue in using like operator on find for Integer data type in Mongoose
Issue in using like operator on find for Integer data type in Mongoose

Time:09-06

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

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?

CodePudding user response:

One option is using $regexMatch:

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

See how it works on the playground example

  • Related