【发布时间】: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