$regex in mongodb pipeline

Viewed 31

I want to be able to use $regex to be able to find any words that contain certain keyword given by user, but it is not working. I have this:

aggregatePipeline.push({
      $match: {
        name: {
          $regex: `/${name.toLowerCase()}/`,
        },
      },
    });

I tried using $regexMatch, but it says it is not supported for free tier. Is there a work around?

{
      $regexMatch: {
        input: "$name",
        regex: `/${name}/`,
      },
}

I'm trying to avoid $regexMatch at least for now due to free tier.

I am able to retrieve user by the exact name, but I also want to retreive all names that have a substring ex. 'ger', should retrieve 'Gerry', 'Gerald', 'anger'....

aggregatePipeline.push({ $match: { name: name.toLowercase() } });

Thanks a lot!

1 Answers

{ 'search field': { $regex: new RegExp('search name', 'gi') } }

Related