title: MongoError: E11000 duplicate key error collection for unique compound index
body: <blockquote>
    <p>Question is related to <code>unique compound</code> index unlike other such questions which have <code>unique</code> index only. I also have <code>sparse: true</code> for the indexes.</p>
  </blockquote>
  
  <p>I've the following indexes in my collection</p>
  
  <pre><code>[
    {
      "v": 2,
      "key": {
        "_id": 1
      },
      "name": "_id_",
      "ns": "somedb.votes"
    },
    {
      "v": 2,
      "key": {
        "answerId": 1
      },
      "name": "answerId_1",
      "ns": "somedb.votes",
      "sparse": true,
      "background": true
    },
    {
      "v": 2,
      "key": {
        "questionId": 1
      },
      "name": "questionId_1",
      "ns": "somedb.votes",
      "sparse": true,
      "background": true
    },
    {
      "v": 2,
      "unique": true,
      "key": {
        "answerId": 1,
        "votedBy": 1
      },
      "name": "answerId_1_votedBy_1",
      "ns": "somedb.votes",
      "sparse": true,
      "background": true
    },
    {
      "v": 2,
      "unique": true,
      "key": {
        "questionId": 1,
        "votedBy": 1
      },
      "name": "questionId_1_votedBy_1",
      "ns": "somedb.votes",
      "sparse": true,
      "background": true
    }
  ]
  </code></pre>
  
  <p>and I've the following document in the collection</p>
  
  <pre><code>{
    "_id": ObjectId("59fdd3ce915511329553dfaa"),
    "updatedAt": ISODate("2017-11-04T14:54:22.110Z"),
    "votedAt": ISODate("2017-11-04T14:50:54.681Z"),
    "questionId": ObjectId("59fc77e45a857465a90339cc"),
    "value": -1,
    "votedBy": ObjectId("59fc4274aa686d39abe5d58a"),
    "type": "QuestionVote",
    "__v": 0
  }
  </code></pre>
  
  <hr>
  
  <p>Now when I try to execute the following</p>
  
  <p><code>db.votes.insert({ questionId: ObjectId("59fc798d5a857465a90339cf"), value: -1, votedBy: ObjectId("59fc4274aa686d39abe5d58a"), type: 'QuestionVote', _id: ObjectId("5a003240bfd8194a02d0add8") })</code></p>
  
  <p>I get the following error</p>
  
  <pre><code>E11000 duplicate key error collection: somedb.votes index: answerId_1_votedBy_1 dup key: { : null, : ObjectId('59fc4274aa686d39abe5d58a') }
  WriteResult({
    "nInserted": 0,
    "writeError": {
      "code": 11000,
      "errmsg": "E11000 duplicate key error collection: somedb.votes index: answerId_1_votedBy_1 dup key: { : null, : ObjectId('59fc4274aa686d39abe5d58a') }"
    }
  })
  </code></pre>
  
  <p>I don't understand the reason. 
  The indexes are <code>sparse</code> and <code>compound</code>. But the error is just because of presence of the same <code>votedBy</code> field.</p>
  
  <p>i.e. Executing the following, </p>
  
  <pre><code>db.votes.insert({votedBy: ObjectId("59fc4274aa686d39abe5d58a")})
  </code></pre>
  
  <p>I get the following error even if there is no explicit indexing on the <code>votedBy</code> object.</p>
  
  <pre><code>E11000 duplicate key error collection: somedb.votes index: answerId_1_votedBy_1 dup key: { : null, : ObjectId('59fc4274aa686d39abe5d58a') }
  WriteResult({
    "nInserted": 0,
    "writeError": {
      "code": 11000,
      "errmsg": "E11000 duplicate key error collection: somedb.votes index: answerId_1_votedBy_1 dup key: { : null, : ObjectId('59fc4274aa686d39abe5d58a') }"
    }
  })
  </code></pre>
  
  <p>Ref:  Compound Index - <a href="https://docs.mongodb.com/manual/core/index-compound/#compound-indexes" rel="noreferrer">https://docs.mongodb.com/manual/core/index-compound/#compound-indexes</a></p>