【发布时间】:2022-02-22 22:15:58
【问题描述】:
我有一个用户模式如下(JavaScript):-
const userSchema = mongoose.Schema({
name: {
type: String,
required: [true, "User must have a name."]
},
password: {
type: String,
required: [true, "User must have a password."],
minLength: 8,
select: false
},
email: {
type: String,
required: [true, "User must have an email."],
unique: true,
lowercase: true,
validate: [validator.isEmail, "Email should be in correct format."]
},
active: {
type: Boolean,
default: true,
select: false
}
}
我想确保电子邮件是唯一的,但仅适用于活动设置为 true 的文档。这甚至可能吗,还是我应该为非活动用户制作另一个集合?
谢谢。
【问题讨论】:
-
你可以使用Partial Indexes。
标签: mongodb mongoose unique unique-index