title: mongoDB/mongoose: unique if not null
body: <p>I was wondering if there is way to force a unique collection entry <strong>but only if entry is not null</strong>.
  e
  Sample schema:</p>
  
  <pre><code>var UsersSchema = new Schema({
      name  : {type: String, trim: true, index: true, required: true},
      email : {type: String, trim: true, index: true, unique: true}
  });
  </code></pre>
  
  <p>'email' in this case is not required but if 'email' is saved I want to make sure that this entry is unique (on a database level).</p>
  
  <p>Empty entries seem to get the value 'null' so every entry wih no email crashes with the 'unique' option (if there is a different user with no email).</p>
  
  <p>Right now I'm solving it on an application level, but would love to save that db query.</p>
  
  <p>thx</p>