Recursive
Recursive

Reputation: 199

$regex in mongodb pipeline

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!

Upvotes: 0

Views: 74

Answers (1)

2218675712
2218675712

Reputation: 31

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

Upvotes: 2

Related Questions