title: Unique Constraint with Two Fields in MongoDB
body: <p>I have a collection with fields "email" and "friends_email". I would like to setup a unique-ness constraint like the following, using MongoDB:</p>
  
  <ol>
  <li><p>No record will have the same value for email and friends_email. So this would be invalid:</p>
  
  <pre><code>{"email": "abc@example.com", "friends_email": "abc@example.com" }
  </code></pre></li>
  <li><p>No 2 records will have the same values for all fields. So the following examples would ALL be invalid:</p>
  
  <pre><code>{
      { "email": "abc@example.com", "friends_email": "def@example.com" },
      { "email": "abc@example.com", "friends_email": "def@example.com" }
  }
  {
      { "email": "abc@example.com", "friends_email": null },
      { "email": "abc@example.com", "friends_email": null }
  }
  {
      { "email": null, "friends_email": "abc@example.com" },
      { "email": null, "friends_email": "abc@example.com" }
  }
  </code></pre>
  
  <p>In plain english, it would be something like, the concatenation of <code>email</code> and <code>friends_email</code> will be unique, with <code>null</code> and <code>undefined</code> being coalesced into empty-string.</p></li>
  </ol>
  
  <p>What's the best way to enforce this rule in MongoDB?</p>