如何确保唯一的电子邮件,userName将使用Mongoose进入MongoDB

时间:2015-02-21 18:28:13

标签: javascript node.js mongodb mongoose

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应该是唯一的,我无法做到这一点。

1 个答案:

答案 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 });