Search code examples
mongodbmongodb-.net-driver

Better ways to handle MongoDB exceptions


I am working on a MongoDB-backed authentication module for my asp.net application. The MongoDB database has a collections of users where I store login information - username, email and password. Both username and email are set to be unique:

users.ensureIndex({email:1}, {unique:1})
users.ensureIndex({uname:1}, {unique:1})

Now, if I try to insert a record with existing uname or email, i get a MongoDB exception:

Safemode detected an error: E11000 duplicate key error index: 
authdb.users.$email_1  dup key: { : "test@test.com" } 
(response: { "err" : "E11000 duplicate key error index: authdb.users.$email_1  
dup key: { : \"test@test.com\" }", "code" : 11000, "n" : 0, "connectionId" : 9, 
"ok" : 1.0 })

I need to tell the user that the username or email they entered already exists, but the whole exception is just a blob of text, and to see what's going on I have to guess by looking whether "$email" or "$uname" are in the error message text. Is there some sort of parser for MongoDB exceptions which would help me detect what the exception is for?


Solution

  • I have created a feature request in MongoDB's bug tracker to add extra fields to the output of getLastError on duplicate key errors. Please vote for it at https://jira.mongodb.org/browse/SERVER-3069.

    Until that is implemented, for your use case you can just check if the err string contains either "email" or "uname" and retrieve the duplicated value from the document you just tried to insert. It's not as elegant as it could be, but it should work for now.