title: Unique sparced compound index in mongoose
body: <p>I have a model</p>
  
  <pre><code>var ReferenceSchema = mongoose.Schema({
      ownerId:{
          type:Number,
          required:true,
          index: true
      },
      boardId:{
          type:Number,
          index:true
      },
      boardCommentId:{
          type:Number,
          index: true
      },
      postId:{
          type:Number,
          index:true
      },
      postCommentId:{
          type:Number,
          index: true
      }
  });
  </code></pre>
  
  <p>that stores different previously founded references. It could be references of different types and should be combination of different values:<br>
  <strong>ownerId</strong> - always<br>
  <strong>boardId + boardCommentId</strong>  OR   <strong>postId + postCommentId</strong></p>
  
  <p>And I have this combined indexes:</p>
  
  <pre><code>ReferenceSchema.index({ ownerId: 1, postId: 1 }, { unique: true,sparse: true });
  ReferenceSchema.index({ postId: 1, postCommentId: 1 }, { unique: true,sparse: true });
  ReferenceSchema.index({ boardId: 1, boardCommentId: 1 }, { unique: true,sparse: true });
  </code></pre>
  
  <p>Problem is when I'm trying to add new document, I have an error <code>E11000 duplicate key error index: db.references.$ownerId_1_postId_1 dup key: { : 33390409, : null }</code> even with <code>sparse:true</code>. Am I using sparse incorrectly or I need another approach with compound indexes and sparse?</p>