problem
I have a main register and x sub-registers which stream data to the main register. I sync these registers during the night with the main register. So during the daytime there will be created data in x1,x2,...xn and at night all the data is sent to the main register.
It is not very probable but it could be that for example x1 and x2 generate the same _id. Now if I sync them the first register would create the document and the second register would upsert (and thus overwriting) my document.
solutions
current
To prevent that I currently save the original _id under a field refId this is very awful because I can't use all the references and most of the functions on my main register.
option I - autoincrement
I planned to switch to autoincrement _ids but I read that this is something you shouldn't do if you plan to scale:
https://blog.serverdensity.com/switching-to-mongodb-and-auto-increment/ https://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/
option II - modifying the sub-register _id
I'm already saving from which subregister my document came from. Thus I could check if an _id exists but has another sub-register. If so I could generate a new _id for the new document and give back the new _id to my sub-register. This sounds easy at a first glance but I do have a lot of references in my documents.
Question)
What is a good solution to handle this issue with possible duplicate ids? is there an easier but effective way to solve this? E.g. prefixing the 24 digits ObjectId automatically.