How to generate unique 8 digit Organization ID in MongoDB?

1.2k views Asked by At

I am working on creating Customer Relationship Management system. I need to create an eight digit numeric, unique ID for a particular client. How can achieve that in Java Script with Mongo DB?

2

There are 2 answers

0
Mexa Fireg On

First of all, you need to know that the collision possibility with just 8 numbers is a little high, so you need to be careful with that if you do not want identical ids.

but you can use this

Math.floor(10000000 + Math.random() * 90000000)

I suggest you use a UUID, but you know more about your project.

0
Loïc Faure-Lacroix On

It's hard or impossible to answer this without more information about the use cases.

Simply put, you could use a auto increment number and that would give you a unique id up to 99,999,999.

But if you want to go further you can store it as a 26bit structure up to 67108863 in base 10.

From there you have to determine the meaning of your identifier.

Let say you plan to have 4 server that can the identifiers.

You could set the first 2 bits to server id. so server 1 would set 00 and server 4 would set 11.

This leaves you with 24 bits to define.

So now you could want to set some granularity... Like setting the year in the format YY because you may not expect this app to live past 2100.

6 bits will let us create a number up to 64 so we can use an extra bit to count up to 128.

So 2020 would be "001 0100".

We only have 12 months a year so it fits into 4 bits.

October is the tenth month so number 9 or "1001" in binary.

We have a maximum of 31 days per month so 5 bits will be good enough

Today is the 15th day of the month so number 14 in binary is "01110"

So far, we used 18 bits to create 1 id per day and that leaves us with 8 more bits.

server id | year   | month  | day   
2 bits    | 7 bits | 4 bits | 5 bits

With 9 remaining bits you can create a maximum of 256 id per day on 4 different server without risking to create any collision.

So if you were to use the maximum amount of ids, per day you'd be able to create 1024 id per day at most. It's not a lot but it can be good enough depending on the needs.

Using that method, the first id on the server 0 for today could look like this:

00 | 001 0100 | 1001 | 0 1110 | 0 0000 0001

Or in base 10: "0539 7505"

So without knowing your requirements, it's impossible to come up with a better identifier that makes some sense. Obviously the time based methods greatly reduce the amount of bits that can be used. Because days in the past are ids that can't be used and unfortunately with 4 bits of data for months the numbers 13,14,15 are never used and have no meaning and are simply list forever.

In the case of the year, we could even save a bit and say instead of 7 bits we'll use 6 bits up to 64 and the number is just 2020 + number so 2020 would be "00 0000" So you could just take current year - 2020 to find the right year to put in the id and that would be good up to 2084 if that's good enough.

Instead of using a date, you could use anything that can have a meaning to build a unique identifier. If you use random numbers, not only the ids doesn't have a meaning but you risk to create collision.

In the end, why don't use simply use an ObjectId created by mongodb? It's a unique identifier and it fits into 12bits so you can always make it a 8 digit number.