title: How to make user's email unique in mongoDB?
body: <p>I am making a collection of user's data. This will be pretty basic consisting of user's email, user's password (hashed), and an array of strings.</p>
  
  <pre><code>{
      email: 'user@example.com',
      password: 'password hash',
      arr: []
  }
  </code></pre>
  
  <p>I want to make sure that there can't be two inserts with the same <code>email</code>.
  I read that the <code>_id</code> of mongoDB is unique by default, and it uses some special data structure for improved performance.</p>
  
  <p>How can I make sure that the <code>email</code> remains unique, and if possible can I leverage the performance benefits provided by the <code>_id</code> field ?</p>
  
  <p>Edit: I also want to make sure that there are no <code>null</code> values for <code>email</code> and that there are no documents which do not contain the <code>email</code> field.</p>