如何确保唯一的电子邮件,用户名将使用 Mongoose 进入 MongoDB [英] How to Ensure a Unique Email, userName will Enter into MongoDB using Mongoose
本文介绍了如何确保唯一的电子邮件,用户名将使用 Mongoose 进入 MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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 应该是唯一的,我无法做到这一点.
Above is the schema I would like to put into MongoDB, but email and userName should be unique and I am unable to do that.
推荐答案
我试过这种方法,它奏效了
I tried this way and it worked
db.UserSchema = new db.Schema({用户名:{类型:字符串,索引:{唯一:真,dropDups:真}},emailId: {type: String, index: {unique: true, dropDups: true}},密码:{类型:字符串,要求:真},联系人号码:字符串,区域:字符串,isActive:布尔值,上次登录日期时间:日期});
这篇关于如何确保唯一的电子邮件,用户名将使用 Mongoose 进入 MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文