title: Mongo sparse index is not working as I expected
body: <p>I said 'as I expected', because I might be misunderstanding how it should work.</p>
  
  <p>I have a model containing objects like this one :</p>
  
  <pre class="lang-json prettyprint-override"><code>{
      "_id" : ObjectId("56408d76ef82679937000008"),
      "_type" : "ford",
      "year" : 1986,
      "model" : "sierra",
      "model_unique" : 1,
      "__v" : 0
  }
  </code></pre>
  
  <p>I need a compound unique index that will not allow to insert two objects with the same <code>_type</code> and <code>model</code> combination unless specified.</p>
  
  <p>The way I thought I could specify that, was using the <code>model_unique</code> column and make the index sparse, so adding the former document twice should fail, whereas the following should be allowed (note that there is no <code>model_unique</code> field):</p>
  
  <pre class="lang-json prettyprint-override"><code>{
      "_id" : ObjectId("56408e0d636779c83700000a"),
      "_type" : "veridianDynamics",
      "year" : 1986,
      "model" : "sierra",
      "__v" : 0
  }
  {
      "_id" : ObjectId("another ID"),
      "_type" : "veridianDynamics",
      "year" : 2003,
      "model" : "sierra",
      "__v" : 0
  }
  </code></pre>
  
  <p>I thought this would work with this index:</p>
  
  <pre class="lang-js prettyprint-override"><code>Schema.index({"_type": 1, "model": 1, "model_unique": 1}, { unique: true, sparse: true });
  </code></pre>
  
  <p>But it is actually failing with:</p>
  
  <pre><code>[MongoError: insertDocument :: caused by :: 11000 E11000 duplicate key error index: mongoose-schema-extend.vehicles.$_type_1_model_1_model_unique_1  dup key: { : "veridianDynamics", : "sierra", : null }]
  </code></pre>
  
  <p>So apparently it is considering that the undefined fields have a null value.</p>
  
  <p>I'm using <code>mongod --version
  db version v2.6.11</code></p>
  
  <p>And <code>npm -v mongoose
  2.14.4</code></p>