4
votes

I have a schema :

  var RegisterInfoSchema= new Schema({
  Organization:String,
  NGOName:String,
  Acronym:String,
  Address:String,
  Province:String,
  District:String,
  Tehsil:String,
  Telephone_number:String,
  Website:String,
  Demographics:String,
  Username:{type:String ,index: {unique:true}},
  Password:String
  })

exports.savePersonalInfo = function (req,res){
console.log("savePersInfo CALLED");

var receivedObj = new RegisterInfo({
    Organization:           req.body.regOrgType ,
    NGOName:                req.body.regName,
    Acronym:                req.body.regAcronym ,
    Address:                req.body.regAddress ,
    Province:               req.body.regProvince,
    District:               req.body.regDistrict,
    Tehsil:                 req.body.regTehsil ,
    Telephone_number:       req.body.regTelNo  ,
    Website:                req.body.regWebAddr,
    Demographics:           req.body.regDemographics,
    Username:               req.body.regUserName ,
    Password:               req.body.regPsw
      });

     receivedObj.save(function(err){
    console.log("inside Save ");
    if(err){                        
        console.log(err);
    }
    else{
        console.log("Saved!!");
        res.send("");

    }

   });
   }

There is indexing in Username When I am trying to save data using save() method then it gives the following error:

{ [MongoError: E11000 duplicate key error index: testdb.registerinfos.$username_1 dup key: { : null }] name: 'MongoError', err: 'E11000 duplicate key error index: testdb.registerinfos.$username_1 dup key: { : null }', code: 11000, n: 0, lastOp: 0, connectionId: 339527, ok: 1 }

2
It might be that _id is unset and so it thinks it is a new rowSammaye
Should i specify id myself? @Sammayeana
You shouldnt need to specify the _id, what code are you using when you get this error? And you are sure this isn't when you try to create a new row? Since if this happens when you try to create a new row then it is becuae of the index ofcSammaye
I have updated a code that uses values to save in the db @Sammayeana
Yea I believe this is becuase you are making a new row each time and the same data is being entered, as such you cannot have duplicate username.Sammaye

2 Answers

4
votes

You have a unique constraint on registerinfos.username and you're trying to save a document that has a value for that field that already exists in the database. I know this because that's what it says in the exception ;) It has nothing to do with _id values.

2
votes

At Sammaye noted, your code is trying to create a new RegisterInfo doc each time savePersonalInfo is called. If you're also using this function to update existing docs you'll need to modify the function to do so.

In pseudo-code:

RegisterInfo.findOne({Username: req.body.regUserName}, function (err, reginfo) {
    if (!err) {
        if (!reginfo) {
            // Doesn't exist, create new doc as you're doing now
        } else {
            // Does exist, update reginfo with the values from req.body and then
            // call reginfo.save
        }
    }
});