Question is related to
unique compoundindex unlike other such questions which haveuniqueindex only. I also havesparse: truefor the indexes.
I've the following indexes in my collection
[
{
"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
}
]
and I've the following document in the collection
{
"_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
}
Now when I try to execute the following
db.votes.insert({ questionId: ObjectId("59fc798d5a857465a90339cf"), value: -1, votedBy: ObjectId("59fc4274aa686d39abe5d58a"), type: 'QuestionVote', _id: ObjectId("5a003240bfd8194a02d0add8") })
I get the following error
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') }"
}
})
I don't understand the reason.
The indexes are sparse and compound. But the error is just because of presence of the same votedBy field.
i.e. Executing the following,
db.votes.insert({votedBy: ObjectId("59fc4274aa686d39abe5d58a")})
I get the following error even if there is no explicit indexing on the votedBy object.
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') }"
}
})
Ref: Compound Index - https://docs.mongodb.com/manual/core/index-compound/#compound-indexes
answerId_1_votedBy_1 dup key: { : null,indicates that youranswerIdwas not supplied in every single document and therefore hasnull, which you asked to be "unique". Even in compound you can still only have one. So what you do exactly the same as all other questions is add "sparse" or a "partial index filter" in modern versions, to cater for the documents where you did not add this value which is part of the compound index. - Neil Lunnsparse. But still i'm getting that error. You may see the indexes list.sparse: trueis present. - abhisekpvotedByfield with two other keys that could make the compound. Essentially this means one user gets 1 vote on an answer, 1 vote on a question, and 1 vote that actually does not get assigned to either. That's what you are enforcing here and don't seem to understand that point. Why you want instead is"_id", "type", "voted_by"where there is one unique index and you dicern on the value of "type" and not the name of fieldanswerId/questionId. That fixed naming and alternation is what causes the problem. - Neil Lunn_idas eitherquestionIdoranswerIdas there can be samequestionIds with differentvotedBys. What should I do and how should I structure? - abhisekp_id. Simply have one field for this value rather than two. - Neil Lunn