gpt4 book ai didi

mongodb - 蒙哥错误: E11000 duplicate key error collection for unique compound index

转载 作者:可可西里 更新时间:2023-11-01 09:33:51 24 4
gpt4 key购买 nike

Question is related to unique compound index unlike other such questions which have unique index only. I also have sparse: true for the indexes.

我的收藏中有以下索引

[
{
"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
}
]

我在集合中有以下文档

{
"_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
}

现在当我尝试执行以下操作时

db.votes.insert({ questionId: ObjectId("59fc798d5a857465a90339cf"), value: -1, votedBy: ObjectId("59fc4274aa686d39abe5d58a"), type: 'QuestionVote', _id: ObjectId("5a003240bfd8194a")02 })

出现以下错误

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') }"
}
})

我不明白原因。索引是稀疏复合。但错误只是因为存在相同的 votedBy 字段。

即执行以下操作,

db.votes.insert({votedBy: ObjectId("59fc4274aa686d39abe5d58a")})

即使 votedBy 对象没有显式索引,我也会收到以下错误。

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') }"
}
})

引用:复合指数 - https://docs.mongodb.com/manual/core/index-compound/#compound-indexes

最佳答案

这可能是因为同一个集合有旧索引,正如使用此方法从控制台检查的那样:

db.votes.getIndexes()

然后放下它们:

db.votes.dropIndexes()

在您定义模式的应用程序中,您应该像这样索引复合索引:

<your_schema_name>.index({
field1: 1,
field2:1,
etc...
},{
unique: true,
sparse: true //N.B:(sparse:true) is not a must for the compound unique indexing to work
});

现在重新启动您的应用程序,最后一个最终所需的复合索引应该可以工作。

额外说明

我在创建唯一索引时发现,您的数据库中已经有违反此创建的记录,它不起作用。

来自 node.js:

从我的 mongoose 驱动程序调试器中,我可以看到驱动程序试图索引

Mongoose: <my_collection>.ensureIndex({ field1: 1, 'field2.xx.xxx': 1, field: 3 }, { unique: true, background: true })

我没有从节点收到任何错误,但是当我使用 getIndexes() 方法从控制台检查时,我没有找到新的索引。

从控制台:

我试着确保索引

db.<my_collection>.ensureIndex({ field1: 1, 'field2.xx.xxx': 1, field: 3 }, { unique: true, background: true })

我收到违反此索引的记录错误

{
"ok" : 0,
"errmsg" : "E11000 duplicate key error collection: <db_name>.<collection_name> index: field1_xxx.xxxx.xxxx_1_filed2_1 dup key: { : \"xxx\", : \"xxxx\", : ObjectId('xxx') }",
"code" : 11000
}

结论

如果有任何记录违反了新的所需索引,唯一索引(复合或非复合)将不起作用,即使您删除所有索引并像我之前提到的那样重试重新索引也是如此。

关于mongodb - 蒙哥错误: E11000 duplicate key error collection for unique compound index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47135231/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com