I have this Mongo document
{
_id: ObjectId('6616851ed50d6d1451fa8176'),
phone: "+7(926)458-54-52",
name: "Aboba"
}
I want to find that document if user inputs 7926
, but also my filter is filtering another field as well.
I am filtering with regex phone or name but phone contains "+" and "()" so if user input is 7926
, it wont be found.
How can I resolve that?
My regex:
{'$regex': f'\w*{request_value}\w*', '$options': 'i'}
I am filtering like that:
{$or: [name: regex, phone: regex}
2
Answers
You can create a sanitized string, named
phoneForSearch
, with all special characters removed first. Then, use your regex search onphoneForSearch
.Mongo Playground
You are advised to "break" the
phone
field into separate fields so it is easier to process and format like this:Mongo Playground
Firstly, I agree with Ray’s answer.
That would allow you to also index and search better against it.
However, if you reeeaally want to just ignore special chars, then you can do something like this:
For phone numbers, separate each digit with
D*
which is "one or more non-digits". First add theD
between all digits in your app/code and then search with that as a regex. So a search for7926
against an unclean phone number could be done as:That also matches against other non-numbers like:
which is why it’s better to NOT do something like that.
Mongo Playground
And if you want to handle that
D
insertion in Mongo, you can do it like this:(updating)