简体   繁体   English

如何在MongoDB中搜索各种电话号码格式?

[英]How to search MongoDB for various phone number formats?

I'm attempting to search my MongoDB for string phone numbers that are all in different formats. 我试图在MongoDB中搜索所有格式不同的字符串电话号码。

Eg (323)704-3234, 3237043234, 323-704-3234,+1-323-704-3234,323.704.3234, etc. 例如(323)704-3234、3237043234、323-704-3234,+ 1-323-704-3234、323.704.3234等。

Is there an operator or regex that I can use that MongoDB provides that allows you to find strings minus special characters? 我可以使用MongoDB提供的运算符或正则表达式来使您找到减去特殊字符的字符串吗?

For example in c#, 例如在c#中

collection.Find(Query.Matches("PhoneNumber",(some regex, replace, or where)3237043234))

Sorry, but you are doing it wrong. 抱歉,您做错了。

You can accept phone strings in any format (if you want so), but before storing them you need to convert them to some specific format. 您可以接受任何格式的电话字符串(如果需要),但是在存储它们之前,您需要将它们转换为某种特定格式。 In your case the best will be just to convert it to number (smaller size to store, to create index). 在您的情况下,最好的办法就是将其转换为数字(存储时要较小,以创建索引)。

Then one more time, when the user asks for a number - he can also provide it in any format, and before you search for it you will convert it to the number. 然后再有一次,当用户要求输入号码时-他还可以提供任何格式的号码,并且在搜索之前,您会将其转换为号码。 (Do not forget to put index on phone number field) Being more user friendly, you can show the ouput in the same format the user asked it. (不要忘了在电话号码字段上加上索引)为了更加方便用户,您可以按用户要求的格式显示输出。

To change all your current phones, just iterate through the database and update each of the numbers. 要更改您当前使用的所有电话,只需遍历数据库并更新每个电话号码即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM