While using MongoDB NodeJS driver (or even mongoose), when inserting a new document, an E11000 error may return indicating a duplicate key collision has occurred.
What if the collection I am inserting into has multiple unique indices and I want to identify which one of them is hit (e.g. to notify the user the user that his username and/or email is already taken)?
This is the error raised after inserting a document with a colliding index:
{
"name": "MongoError",
"message": "E11000 duplicate key error index: db.users.$_id_ dup key: { : \"blucell\" }",
"driver": true,
"index": 0,
"code": 11000,
"errmsg": "E11000 duplicate key error index: db.users.$_id_ dup key: { : \"blucell\" }"
}
The only notion of the index causing the problem is in the message, which changes over time (already differs between mongo 2.x and 3.x).
Is there any more future-proof/easier way of checking which element/index is causing the collision?