title: Querying MongoDB (via pymongo) in case insensitive efficiently
body: <p>I'm currently creating a website in python (pyramid) which requires users to sign up and log in.  The system allows for users to choose a username which can be a mixture of capital letters, lowercase letters, and numbers.</p>
  
  <p>The problem arises when making sure that two users don't accidentally share the same username, i.e. in my system 'randomUser' should be the same as 'RandomUser' or 'randomuser'.</p>
  
  <p>Unfortunately (in this case) because Mongo stores strings as case sensitive, there could potentially be a number of users with the 'same' username.</p>
  
  <p>I am aware of the method of querying mongo for case insensitive strings:</p>
  
  <pre><code>db.stuff.find_one({"foo": /bar/i});
  </code></pre>
  
  <p>However, this does not seem to work in my query method using pymongo:</p>
  
  <pre><code>username = '/' + str(username) + '/i'
  response = request.db['user'].find_one({"username":username},{"username":1})
  </code></pre>
  
  <p>Is this the correct way of structuring the query for pymongo (I'm assuming not)?</p>
  
  <p>This query will be used whenever a user account is created or logged in to (as it has to check if the username exists in the system).  I know it's not the most efficient query, so should it matter if it's only used on log ins or account creation?  Is it more desirable to instead do something like forcing users to choose only lowercase usernames (negating the need for case-insensitive queries altogether)?</p>