Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoError,err:E11000 duplicate key error

I have a MongoDb schema like this

    var User = new Schema({     "UserName": { type: String, required: true },     "Email": { type: String, required: true, unique: true },     "UserType": { type: String },     "Password": { type: String } }); 

I am trying to create a new user This is done in NodeJs using mongoose ODM And this is the code for creating:

    controller.createUser = function (req, res) {      var user = new models.User({         "UserName": req.body.UserName.toLowerCase(),         "Email": req.body.Email.toLowerCase(),         "UserType": req.body.UserType.toLowerCase()     });     models.User.findOne({ 'Email': user.Email }, function (err, olduser) {                     if (!err) {                         if (olduser) {                             res.send({ 'statusCode': 409, 'statusText': 'Email Already Exists' });                         }                         else if (!olduser) {                             user.setPassword(req.body.Password);                             user.save(function (err, done) {                                 if (!err) {                                     console.log(user);                                     res.send({ 'statusCode': 201, 'statusText': 'CREATED' });                                 }                                 else {                                     res.send({ 'Status code': 500, 'statusText': 'Internal Server Error' });                                 }                             });                         }                     }                     else {                         res.send({ 'statusCode': 500, 'statusText': 'ERROR' });                     }                 }); }; 

The for creating new user,I am giving attributes and values as follows:

 { "UserName": "ann", "Email": "[email protected]", "UserType": "normaluser", "Password":"123456" } 

And I am getting error like this:

{"Status code":500,"statusText":"Internal Server Error","Error":{"name":"MongoError","err":"E11000 duplicate key error index: medinfo.users.$UserName_1  dup key: { : \"ann\" }","code":11000,"n":0,"connectionId":54,"ok":1}} 

I understand that this error is because UserName is duplicated ,but I haven't set UserName with unique constraint.Whenever I add a new row,I need only email to be unique,UserName can be repeated.How to achieve this??

like image 371
dany Avatar asked Nov 19 '12 12:11

dany


1 Answers

@ManseUK Is probably right, that looks like UserName is a 'key' - in this case an index. The _id attribute is the "primary" index that is created by default, but mongodb allows you to have multiple of these.

Start a mongo console and run medinfo.users.getIndexes()? Something must have added an index on 'UserName'.

required: true wouldn't do that, but you might have played with other settings previously and the index hasn't been removed?

like image 167
rdrey Avatar answered Sep 21 '22 18:09

rdrey