我有一个架构:
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("");
}
});
}
用户名中有索引当我尝试使用 save() 方法保存数据时,它会给出以下错误:
{ [MongoError: E11000 重复键错误索引:testdb.registerinfos.$username_1 dup key: { : null }] name: 'MongoError', err: 'E11000 重复键错误索引: testdb.registerinfos.$username_1 dup key: { :空}',代码:11000,n:0,lastOp:0,connectionId:339527,ok:1}