how to index a username in mongo with case-insensitively?

Viewed 5293

I am coding a webservice where users can pick a username that has to be case-insensitive unique. However I want to allow them to use a case-sensitive version of their username.

What is the best way to check at insert that the username doesn't have a case-insensitive duplicate? I currently see 2 ways of doing this:

  • storing both a lowercase version and another version with the case entered by the user, and indexing only the lowercase version
  • store only the version with the case entered by the user and lowercase it for comparison, which defeats the purpose of the index I suppose

Is there a better way?

4 Answers
Related