Mongodb duplicate key error. How do i get the error field from the error object as object?

When trying to enter a new document in mongo with a value on a field that already exist in another document i get this when i iterate through the error object:

for(var att in err){
    console.log(att+": "+err[att]);
}

name: MongoError err: E11000 duplicate key error index: draw.users.$email_1 dup key: { : "spam@online.no" } code: 11000 n: 0 ok: 1

So it tells me what i want to know, the problem is the email field. But can I get the offending field as a key/value rather than just a string?

I use a regular expression. Like this

if(err){
   field = err.err.match(/\$(.*?)_/)[1]
}

Which is totally hacky but is working for me.

If it collides, then making a find() for that query will return you the collision objects and you go from there.