0

I want to execute SQLite query SELECT* FROM account WHERE name LIKE '%$name%' in MongoDB using Node.js using Mongoose. I already wrote code for implementing this query in MongoDB, but it gave me an error.

This is my code:

async(req,res)=>{
    try{
        personname=req.params.personname;
     const newaccount=await account.find({personName:new RegExp('/'+personname+'/')});
     res.status(201).json({
        status:'success',
        result:newaccount.length,
        data:{
           account: newaccount
        }
    })
    }catch(error){
        res.status(404).json({
            status:'fail',
            message:error.message
        })
    }

error:

{
    "status": "fail",
    "message": "Cast to ObjectId failed for value \"maliha\" (type string) at path \"_id\" for model \"account\""
}
Mark Rotteveel
  • 90,369
  • 161
  • 124
  • 175
  • 1
    It looks like this is a problem with loading the result into a mongoose model object. Try turning that off with `{ _id: false }` in the schema options. Or alternatively store ObjectId values in `_id` instead of string values. – AJcodez Aug 15 '21 at 06:11

0 Answers0