There are several users in my application, each user has documents. Each document should have a serial number, which can look something like this: 2013-1, 2013-2 (year and serial number) or, possibly, a prime number: 1, 2, 3 ...
I am currently assigning a sequence number from user preferences when creating a Mongoose document. Based on this serial number and number format from user settings, I create the final document number.
I realized that when 2 documents are created at the same time, they will receive exactly the same number, because I increase the sequence number in the settings immediately after saving the document. But I assign a sequence number when I create (not yet save) a document, so the sequence number will be the same for both documents.
I obviously need a way to handle this serial number with automatic increment at the time of saving ...
How can I assure that this number is unique and automatically incremented / generated?
mongodb mongoose
ragulka
source share