Home > Blockchain >  partial search by number in mongodb node js
partial search by number in mongodb node js

Time:01-26

Hi i have a model which contains OrderNumber:540. I have to add partial search in params like if i send 5 in params it should shows all the results where OrderNumber started with 5. i found the solution with $where but i am getting an error of error MongoError: $where not allowed in this atlas tier

i have an api like /getPurchaseOrdersBySupplierId/:id/:po if po is 5 then it must show all the results that is starting with 5...

OrderNumber: req.params.po})```
please suggest me the method for partial search by int. $where is not working in my case. need some other suggestion

CodePudding user response:

You can use $toString to parse values to string and then search using $regex like this:

db.collection.aggregate([
  {
    "$addFields": {
      "key": {
        "$toString": "$key"
      }
    }
  },
  {
    "$match": {
      "key": {
        "$regex": "^" req.params.po
      }
    }
  }
])

Example here

  •  Tags:  
  • Related