Mongo Error E11000 get key name using node.js

Viewed 226

I am creating an index whose having a unique index and I am trying to insert documents in that collection but using the insertMany option. I am also using Mongo Transaction also. When I am trying to insert a duplicate document in MongoDB it giving me the following index error:

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)]: {}
}

it is giving me information about my unique index but I want the error key number field here so How I can get this?

Also as I am using MongoDB Transaction with insertMany query with ordered: false option but still it is showing error for a single field, not all fields which having duplicate values for e.g: https://docs.mongodb.com/manual/reference/method/db.collection.insertMany/#unordered-inserts.

I need all errors of Insert Many and in a proper format. I am using Transaction. When I am using insertMany without transaction and order false it gives me error like

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

it showing all failed fields details so How I get this using Transaction

Code sample

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": "testing", "locale": "en-us", "path": "number", "value": "400", "deleted_at": false, }], {

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

Kindly help me!

1 Answers

This is from Mongo Documenation:

Excluding Write Concern errors, ordered operations stop after an error, while unordered operations continue to process any remaining write operations in the queue.

Write concern errors are displayed in the writeConcernErrors field, while all other errors are displayed in the writeErrors field. If an error is encountered, the number of successful write operations are displayed instead of a list of inserted _ids. Ordered operations display the single error encountered while unordered operations display each error in an array.

You can see in the documentation that in unordered ops the mongo drive keeps the updating the other operations and respond with write Concern what is causing an issue. In your case _id: 1234 is duplicate.

Try to add writeConcerns and you will be able to see all the duplicate Ids in a form of an array. Check this link.

Related