-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to get index name on duplicate document 11000 error? #2129
Comments
The best way right now is by parsing the error message (slightly hacky, yes). The error message looks like:
Looks pretty consistent across mongodb server 2.4 and 2.6. The |
@vkarpov15 here's how I did it, in case anyone else wants to do this, or if you would like to add it to the wiki, documentation, or Readme perhaps -- it is rather useful for error handling. // this example is showing how to get the field `email` out of the err message
var field = err.message.split('index: test.')[1].split('.$')[1]
// now we have `email_1 dup key`
field = field.split(' dup key')[0]
field = field.substring(0, field.lastIndexOf('_')) // returns email I am using this directly in the error handler component of my new framework called Igloo. https://github.com/niftylettuce/igloo The commit with it: https://github.com/niftylettuce/igloo/commit/3cdee8dcc28b6373a80a4e0c16c3f2c34edfca6c |
@niftylettuce Can we not get it this way?
This way, one does not have to know the database name. EDIT on 31 January, 2015: That breaks when the field name has an underscore in it. Use this instead. var field = err.message.split('.$')[1];
// now we have `email_1 dup key`
field = field.split(' dup key')[0];
field = field.substring(0, field.lastIndexOf('_')); // returns email |
@vkarpov15 After Mongoose 4.x, the error format seems to have changed a bit, and no longer contains the offending field name. The error message, for example, on a field with
Is there a way to get the field that is causing this error? |
@paambaati that's not a mongoose issue, that string is generated by the mongodb server and is specific to the WiredTiger storage engine in mongodb 3.0.0 and 3.0.1. This bug was reported in the core server jira project, fixed, and the fix is in mongodb 3.0.2 |
Thanks to @paambaati I got the functionality I needed. var user = new User(req.body);
user.save(function(error) {
if (error) {
if (error.code === 11000) {
// email or username could violate the unique index. we need to find out which field it was.
var field = error.message.split(".$")[1];
field = field.split(" dup key")[0];
field = field.substring(0, field.lastIndexOf("_"));
req.flash("errors", [{
msg: "An account with this " + field + " already exists."
}]);
res.redirect("/join");
return;
}
throw error;
}
req.flash("messages", "Thank you for joining.");
res.redirect("/");
}); But is there a terser way to get the same result? //cc @vkarpov15 |
I wrote a module to extract the duplicate field. I hope someone finds it useful ❤️! |
Good job @alexbooker! There's also another plugin for that, see #2284 and https://www.npmjs.com/package/mongoose-beautiful-unique-validation |
@vkarpov15 Dude, thank you! No more ugly 11000 and 11001 checks! |
Thank @alexbooker and co, they wrote the plugins :) |
Not sure if anyone still want to extract the value. here is my code
|
And here i wrote mine to get both value and key
|
Here is my RegExp solution to get index name with any type of error syntax I have found: var regex = /index\:\ (?:.*\.)?\$?(?:([_a-z0-9]*)(?:_\d*)|([_a-z0-9]*))\s*dup key/i,
match = error.message.match(regex),
indexName = match[1] || match[2]; Here what I have tested: ('E11000 duplicate key error collection: db.users index: name_1 dup key: { : "Kate" }').match(regex)[1]; // "name"
("E11000 duplicate key error index: myDb.myCollection.$id dup key: { : ObjectId('57226808ec55240c00000272') }").match(regex)[2] // "id"
('E11000 duplicate key error index: test.collection.$a.b_1 dup key: { : null }').match(regex)[1] // "b"
('E11000 duplicate key error collection: upsert_bug.col index: _id_ dup key: { : 3.0 }').match(regex)[1] // "_id" |
My two cents:
MongoDB: |
@Fire7 Thanks for your solution. I have, however, a compound unique index for say a Using your solution gives me Thanks! |
Update 2019, for any one who wanna to use the plugin mongoose-beautiful-unique-validation be aware of this issue when use mongodb 3.6 to up |
you guys can just use err.message.indexOf( 'theFieldInQuestion_1' ) === -1 |
How can I get the index for which the error occurs with?
e.g. if my index is named
email
, how do I get that from theerr
object?The text was updated successfully, but these errors were encountered: