2

I worked with mongo before 3 years and now I'm back in Mongo and I don't know why today search ('%LIKE%' in SQL) is not working.

What I tried is as below.

{ "title": /${req.body.search}/i }

I searched a lot I all most tried all the way but don't know it seems not bed of roses for me.

Note: If I put static text then it's working but I need dynamic as per user search in my app.

Any help would be much appreciated.

Sachin Shah
  • 4,503
  • 3
  • 23
  • 50

1 Answers1

1

MongoDB exposes as $regex operator to allow you search for items.

You could handle it like this:

// in your search handler
const searchTerm = new RegExp(req.body.search, "i")
Model.find({ "title": { $regex: searchTerm } })
PsiKai
  • 1,803
  • 1
  • 5
  • 19