Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7617639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:12:00+00:00 2026-05-31T03:12:00+00:00

I’m currently developing a registration system prototype. It’s very simplistic and essentially is just

  • 0

I’m currently developing a registration system prototype. It’s very simplistic and essentially is just a .NET form which gets written into MongoDB.

What I’m stuck with is an efficient way to generate a unique id/key for each user. These ids must be human friendly so something like a 7 character long alphanumeric string e.g. A1B2C3X.

The solutions I’ve seen so far just use a simple function to generate a random string and then check the database to see if it is unique (and if not repeat untill you find one that is unique). This of course will become more and more computationally expensive as the number of database entries grows.

My idea is to precompute the unique id set and store that in another database. Then when I need to add a new entry into the user database I can “pop” an id from my id databse (in constant time) and know that it does not already exist in the user database without the need to search it.

I’m sure somebody must have done something like this before. Is there a better way? I don’t know why I’m struggling so much with this. Your input is very much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:12:02+00:00Added an answer on May 31, 2026 at 3:12 am

    Generating a random string in the application and checking if it’s unique is not a bad solution. Don’t worry about it being inefficient, it’s not — and definitely not compared to the alternatives. It will certainly be faster than running db.user.count() or keeping a separate table with precalculated IDs. You just need to do it right.

    First of all, how often will new users be created? Probably not very often compared to other things, so really the whole efficiency discussion is moot. Secondly, with 7 characters A-Z, 0-9 that’s a range of 36^7 or somewhere around 78 billion. It will be some time before you will start seeing collisions, to say the least.

    If you just do it like this, it will not incur any performance penalties unless there’s a collision (which is extremely unlikely):

    • Generate a unique user ID
    • Insert your user object, using the user ID as the value of _id
    • Check for duplicate key errors (how to do this depends on the language and driver, but might involve running the getLastError command).
    • On a duplicate key error start over by generating a new user ID

    This way there will only be extra work in the event of a collision (and I really, really want to stress how incredibly unlikely that will be).

    There’s another way of generating a unique user ID: take the current UNIX timestamp (down to the second), append a hash of the hostname and then the process ID, and finally the current value of a counter. This is in fact how Mongo’s ObjectId is generated, and guarantees that you can generate as many objects per second, per process, as the max value of your counter (which in Mongo is 3 bytes, so 16 million). See the docs on ObjectId if you’re interested in the details: http://www.mongodb.org/display/DOCS/Object+IDs

    It has the property that your user IDs will naturally sort in order of creation, but it’s 12 bytes long, so a bit longer than your 7 chars, unfortunately. You can use the same method and skip the hostname/pid, and shorten the counter (which can also be a random number if you like) to two bytes, then you would be down to 6 bytes, which could probably be squeezed into about 9 chars A-Z, 0-9.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.