MongoDB DuplicateKeyException

MongoDB DuplicateKeyException,mongodb,duplicates,mongodb-.net-driver,Mongodb,Duplicates,Mongodb .net Driver,我在尝试向收藏中插入新条目时遇到上述错误。让我困惑的是,我的密钥是一个Guid id字段。实体有AppId和UserId字段,但这些字段不应该是键,也不应该是唯一的 就在我保存之前,Id是全零。设置为唯一Guid后,但save调用抛出MongoDuplicateKey错误。 也许是因为我对Mongo还不熟悉,但我不明白这一点,如果有任何帮助,我们将不胜感激 更新 get索引的输出 WriteConcern detected an error 'insertDocument :: caused b

我在尝试向收藏中插入新条目时遇到上述错误。让我困惑的是,我的密钥是一个Guid id字段。实体有AppId和UserId字段,但这些字段不应该是键,也不应该是唯一的

就在我保存之前,Id是全零。设置为唯一Guid后,但save调用抛出MongoDuplicateKey错误。 也许是因为我对Mongo还不熟悉,但我不明白这一点,如果有任何帮助,我们将不胜感激

更新

get索引的输出

WriteConcern detected an error 'insertDocument :: caused by :: 11000 E11000 duplicate key error index: develop.Test.$AppId_1_UserId_1_Type_1__sub_1__key_1  dup key: { ... }'. (Response was { "ok" : 1, "code" : 11000, "err" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: develop.Test.$AppId_1_UserId_1_Type_1__sub_1__key_1  dup key: {...}).
您在
AppId
UserId
Type
sub
字段上有一个错误,这就是您出现此错误的原因

{
    "0" : {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "develop.Test"
    },
    "1" : {
        "v" : 1,
        "unique" : true,
        "key" : {
            "AppId" : 1,
            "UserId" : 1,
            "Type" : 1,
            "_sub" : 1,
            "_key" : 1
        },
        "name" : "AppId_1_UserId_1_Type_1__sub_1__key_1",
        "ns" : "develop.Test"
    },
    "2" : {
        "v" : 1,
        "key" : {
            "Type" : 1,
            "_sub" : 1,
            "_g" : 1
        },
        "name" : "Type_1__sub_1__g_1",
        "ns" : "develop.Test"
    }
}
现在如何解决这个问题呢

  • 如果不是你创造的,也许是你的同事或其他人创造的。在这种情况下,如果不与他们交谈,就不能删除索引
  • 您可能希望使用方法删除索引


你能给我们看一下
db.collection.getIndexes()的输出吗
结果是我不得不一个键一个键地切换!subject字段有一个[bsonement(“\u sub”)]标记,但Key没有标记,因此\u Key始终为null。谢谢你给我指明了正确的方向!
"1" : {
        "v" : 1,
        "unique" : true,
        "key" : {
            "AppId" : 1,
            "UserId" : 1,
            "Type" : 1,
            "_sub" : 1,
            "_key" : 1
        },
        "name" : "AppId_1_UserId_1_Type_1__sub_1__key_1",
        "ns" : "develop.Test"
    },
db.collection.dropIndex({ AppId: 1, UserId: 1, Type: 1, _sub: 1, _key: 1 })