Advice storing phone number

Hello,

I’m designing a mongodb with a friend for a small project and I was wondering if there is any good habits concerning storing the phone number?
Should I just put a String or divide the phone number into 2 the country code + phone number ?

Hi @Yoni_40785,

Typically phone numbers are stored as text and in my experience, the storage format depends on a couple of requirements/variables:

  1. Source (i.e. from your UI and/or is it going to imported from an external source as well)
  2. Does it contain International numbers, extension number etc?
  3. Will you be searching against this field or performing some analytics? Does it need to be indexed?
  4. Do you need to know the country the number belongs to? I.e. International Dialing Code or for the purpose of analytics. This is somewhat linked to 3.

So if you have answers to these questions you’ll be able to come up with a solution that meets those requirements. If you don’t have all the answers yet, you can begin with creating two fields; first field to store the original input string and the second string that strips out any non numeric characters (i.e. convert the first “+” to 00, remove hyphens, parenthesis, dots etc), and index the second. This way you can drop/refine one column if the requirements are a lot less.

In terms of actual schema format, you could use a sub-document of arrays with labels for each phone type (mobile, tel 1, home, work etc). The Attribute Pattern can come in handy here too… depending on the requirements.

Some of the other guys (@steevej-1495 and @Ramachandra_37567) may have their own ways of dealing with phone numbers.

I have use string and the Attribute Pattern for phone numbers.

And what have you done with the country code ?

I would be storing 3 phones per user

I was thinking of using :

"phones":[
{
 "countryCode": "<String>",
 "phoneNumber": "<String>"
}]

Could I use the attribute pattern this way:

"phones":[
  {"k":"countryCodePhone1", "v": "<String>"},
  {"k":"phoneNumber1", "v":"<String>"}
]

and then iterate over the name ?

No special treatment for the country code as the phone numbers were for human to read only.

phones : 
[
    {
        tags : [ "string" ] ,
        number : "string" ,
        remark : "string"
    }
]

example

phones : 
[
    {
        tags : [ "home" ] ,
        number : "514994xxxx" ,
    } ,
   {
      tags : [ "office" , "daytime" ] ,
      number : "8199999xxx" ,
      remark : "Do not leave message call cell"
   }
   {
      tags : [ "cell" ] ,
      number : "...." ,
      remark : "If weekend and urgent try boat"
   }
   {
      tags : [ "boat" ] ,
      number : "...." ,
      remark : "Urgent only"
   }

]

Oh okay I see ! Thanks for your help

I intend to do a verification check via the phone number by sending a message to the phone and letting the user enter the code he just received, it’ll be use only once to verify the user.