【问题标题】:Mongo Error E11000 get key name using node.jsMongo 错误 E11000 使用 node.js 获取密钥名称
【发布时间】:2021-06-09 23:40:38
【问题描述】:

我正在创建一个具有唯一索引的索引,并且我正在尝试在该集合中插入文档,但使用 insertMany 选项。我也在使用 Mongo Transaction。当我尝试在 MongoDB 中插入重复文档时,它给了我以下索引错误:

MongoError: E11000 duplicate key error collection: demo.unique_values index: unique_name dup key: { : "1234", : "testing", : "en-us", : "number", : 400, : false }
    
  operationTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1623229996 },
  ok: 0,
  errmsg: 'E11000 duplicate key error collection: demo.unique_values index: unique_name dup key: { : "1234", : "testing", : "en-us", : "number", : 400, : false }',
  code: 11000,
  codeName: 'DuplicateKey',
  '$clusterTime': {
    clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1623229996 },
    signature: { hash: [Binary], keyId: 0 }
  },
  [Symbol(mongoErrorContextSymbol)]: {}
}

它向我提供了有关我的唯一索引的信息,但我想要这里的错误键号字段,所以我怎样才能得到这个?

此外,我正在使用带有 insertMany 查询的 MongoDB Transaction 和 ordered: false 选项,但它仍然显示单个字段的错误,而不是所有具有重复值的字段,例如:https://docs.mongodb.com/manual/reference/method/db.collection.insertMany/#unordered-inserts

我需要 Insert Many 的所有错误并且格式正确。我正在使用事务。 当我使用 insertMany 没有交易和订单 false 时,它​​给了我类似的错误

{
    "name": "BulkWriteError",
    "driver": true,
    "code": 11000,
    "writeErrors": [{
        "code": 11000,
        "index": 0,
        "errmsg": "E11000 duplicate key error collection: demo.unique_values index: unique_name dup key: { : \"1234\", : \"testing\", : \"en-us\", : \"number\", : \"400\", : false }",
        "op": {
            "key": "1234",
            "type_uid": "testing",
            "locale": "en-us",
            "path": "number",
            "value": "400",
            "deleted_at": false,
            "_id": "60c08ad651ab70014397dbab"
        }
    }, {
        "code": 11000,
        "index": 1,
        "errmsg": "E11000 duplicate key error collection: demo.unique_values index: unique_name dup key: { : \"1234\", : \"testing\", : \"en-us\", : \"title\", : \"tiger\", : false }",
        "op": {
            "key": "1234",
            "content_type_uid": "testing",
            "locale": "en-us",
            "path": "title",
            "value": "tiger",
            "deleted_at": false,
            "_id": "60c08ad651ab70014397dbac"
        }
    }],
    "result": {
        "ok": 1,
        "writeErrors": [{
            "code": 11000,
            "index": 0,
            "errmsg": "E11000 duplicate key error collection: demo.unique_values index: unique_name dup key: { : \"1234\", : \"testing\", : \"en-us\", : \"number\", : \"400\", : false }",
            "op": {
                "key": "1234",
                "type_uid": "testing",
                "locale": "en-us",
                "path": "number",
                "value": "400",
                "deleted_at": false,
                "_id": "60c08ad651ab70014397dbab"
            }
        }, {
            "code": 11000,
            "index": 1,
            "errmsg": "E11000 duplicate key error collection: demo.unique_values index: unique_name dup key: { : \"1234\", : \"testing\", : \"en-us\", : \"title\", : \"tiger\", : false}",
            "op": {
                "key": "1234",
                "ype_uid": "testing",
                "locale": "en-us",
                "path": "title",
                "value": "tiger",
                "deleted_at": false,
                "_id": "60c08ad651ab70014397dbac"
            }
        }],
        "writeConcernErrors": [],
        "insertedIds": [{
            "index": 0,
            "_id": "60c08ad651ab70014397dbab"
        }, {
            "index": 1,
            "_id": "60c08ad651ab70014397dbac"
        }],
        "nInserted": 0,
        "nUpserted": 0,
        "nMatched": 0,
        "nModified": 0,
        "nRemoved": 0,
        "upserted": [],
        "lastOp": {
            "ts": "6971724857717293057",
            "t": 606
        }
    }
 }

它显示了所有失败的字段详细信息,所以我如何使用 Transaction 获得它

代码示例

try{
let session = await adaptor.startSession({});
await session.withTransaction(async () => {
    await adaptor.updateMany("unique_values",
                    {
                        key:{{key}},
                        uid:{{uid}},
                        locale:{{locale}},
                        type_uid:{{type_uid}},
                        deleted_at:false
                    }, {
                        $set:{
                        deleted_at:true
                    }},{session})
                    await adaptor.insertMany("unique_values",[{

"key":"1234", "type_uid": "测试", “语言环境”:“en-us”, “路径”:“编号”, “价值”:“400”, “deleted_at”:假, }], {

                        ordered: false
                    },
                    
                    );
                    await adaptor.deleteMany("unique_values",{
                        key:{{key}},
                        uid:{{uid}},
                        locale:{{locale}},
                        type_uid:{{type_uid}},
                        deleted_at:false
                    },{session})
})
}catch(error){
    
}

请帮帮我!

【问题讨论】:

    标签: node.js mongodb mongoose mongodb-query


    【解决方案1】:

    这是来自 Mongo 文档:

    排除 Write Concern 错误,有序操作在一个 错误,而无序操作继续处理任何剩余的 在队列中写入操作。

    写入关注错误显示在 writeConcernErrors 字段中, 而所有其他错误都显示在 writeErrors 字段中。如果 遇到错误,成功写操作的次数是 显示而不是插入的 _id 列表。有序操作 显示无序操作时遇到的单个错误 在数组中显示每个错误。

    您可以在文档中看到,在无序操作中,mongo 驱动器会不断更新其他操作,并以 write Concern 响应导致问题的原因。在您的情况下,_id: 1234 是重复的。

    尝试添加writeConcerns,您将能够以数组的形式看到所有重复的Id。检查this链接。

    【讨论】:

    • 我们如何在 insert many for transaction 中编写 Concern?
    • 如果可能的话,请您分享一个这样的例子
    • 问题在于您尝试写入的数据,如果您可以添加,我将添加一个。从错误堆栈看来,我提到的 id 出现了两次。
    • 更新了问题,您可以检查一下吗?并帮助我
    • 请在这里帮助在 let session = await adapter.startSession({}); 中添加 write Concern;
    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 2014-01-26
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-07
    相关资源
    最近更新 更多