title: How to auto generate a custom ID for each record in a MongoDb collection?
body: <p>The MERN application that I'm trying to develop has a requirement that each record in the collection should have custom unique ID that should be auto generated each time when a new record is added.</p>
  
  <p>Examples of the how required IDs shoudl look like: </p>
  
  <p>IDs for employees would look like: EMP00001, EMP00002, EMP00003...</p>
  
  <p>IDs for Branches would look like: BR00001, BR00002, BR00003... etc.</p>
  
  <p>Please note that this should be auto saved with each record.</p>
  
  <p>I didn't find any satisfactory help from MongoDB docs (<a href="https://www.mongodb.com/blog/post/generating-globally-unique-identifiers-for-use-with-mongodb" rel="nofollow noreferrer">https://www.mongodb.com/blog/post/generating-globally-unique-identifiers-for-use-with-mongodb</a>) where the available options to create custom identifiers doesn't fulfill my requirements. </p>
  
  <p>I've also read that it is not ideal to create IDs other that MongoDb's own ObjectIds, but i don't understand how it would address the unique business requirements where records needs to have a simple understandable IDs not some random junk of digits.</p>
  
  <p>I would appreciate if you could help with any official solution or if there's a work around that could help in achieving the above mentioned requirement.</p>