Home > Enterprise >  Adding hyphens to regex string returns 0 results-MongoDB
Adding hyphens to regex string returns 0 results-MongoDB

Time:09-09

I'm currently running a query on MongoDB, the field I'm querying contains values such as 123245123 - Johnson-Perez. If I build a query such as {"name":/123245123/ix} I get tons of rows and that's fine, however, as soon as you add the hypens {"name":/123245123 - Johnson-Perez/ix} I get 0 rows. Do you guys know what could be causing this? I've tried replacing the hyphens with [-] and putting the entire string in double quotes but it didn't work. I have zero knowledge on regex so I don't know what could be causing this.

CodePudding user response:

Try:

{"name":/^[0-9]* - [A-Zaz]*$/ix}

CodePudding user response:

Solved it, I have to escape the whitespaces as well as the hyphen and the dots.

  • Related