Well I want to do some basic error handling and return the _id of the bad insert.
When I am in mongo console and try to insert a duplicate document, then this error is thrown:
> db.users.insertOne({ firstname: "", lastname: "" })
2020-04-27T21:27:55.958+0200 E QUERY [js] WriteError({
"index" : 0,
"code" : 11000,
"errmsg" : "E11000 duplicate key error collection: clay.users index: lastname_1_firstname_1 dup key: { lastname: \"\", firstname: \"\" }",
"op" : {
"_id" : ObjectId("5ea7323b70f6a7fdfba31e24"),
"firstname" : "",
"lastname" : ""
}
})
A very clear duplicate error, with the faulty "op" and the responding _id. When I am doing this with the mongo nodejs driver, I can only get this error:
driver: true,
name: 'MongoError',
index: 0,
code: 11000,
keyPattern: { lastname: 1, firstname: 1 },
keyValue: { lastname: 'Moon', firstname: 'Sania' },
errmsg: 'E11000 duplicate key error collection: clay.users index: lastname_1_firstname_1 dup key: { lastname: "Moon", firstname: "Sania" }',
[Symbol(mongoErrorContextSymbol)]: {}
I can check for the error code and stuff, but cant access the "op" property with the faulty "_id" for handling. It would benefit my error handling and code greatly if I could access the faulty "_id". Kind regards