'Generate Unique Sequential ID (starting from 1) with multi-user - Javascript
The application that I am building is being used by multiple users.
I am having a problem upon generating a unique ID.
My code runs like this: it will query the mongoDB for the length of all data and will add one to it so it will become unique and sequential. (For example, the database already have 40 data on it, now when the user clicked the save button, the function will get the length of the data which is 40 and it will add 1 to it. So the ID of the data will be 41.)
I encountered 2 problems. First, when the 2 user clicked the save button at the same exact time, both the data they created will have an ID of 41. Second, when the user doubled click the save button, it will still generate 2 data with both 41 as their ID.
I looked for an answer and I saw UUID but I need sequential ID that starts from 1 to infinite.
Can someone help me?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
