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!