Thursday, 27 April 2017

How do you apply regex to value in database to query against in mongodb?

Here is the scenario, you are storing phone numbers in mongo as a string. Values could be any of the following:

  • 987-654-3210
  • 9876543210
  • (978) 654-3210

All of these numbers are the same. I want to search using a RegExp which will basically strip the value of all non alphanumeric characters. Searching using 987-654-3210 will return 9876543210, but that's it. I'm hoping to find a solution where the regex will be applied to what is stored in the database to match the regexp that I'm passing in the query.

Ultimately, if all three of those phone numbers are in the database and the user searches using any of the three, all three should be returned.



via Jacques Gingras

No comments:

Post a Comment