db.UserSchema = new db.Schema({
userName: {type:String,
unique:true,
required:true,index:true},
emailId: {type:String,
unique:true,
required:true,index:true},
password: {type:String,
required:true},
contactNumber: String,
region: String,
isActive:Boolean,
lastLoginDateTime:Date
});
上面是我想要放入MongoDB的架构,但是email和userName应该是唯一的,我无法做到这一点。
答案 0 :(得分:1)
我试过这种方式并且有效
db.UserSchema = new db.Schema({
userName: {type: String, index: {unique: true, dropDups: true}},
emailId: {type: String, index: {unique: true, dropDups: true}},
password: {type:String,
required:true},
contactNumber: String,
region: String,
isActive:Boolean,
lastLoginDateTime:Date
});