0

我想存储一个电子邮件字符串和密码,其中只有 1 需要是唯一的。因此,某人可以使用具有 2 个不同密码(或 100 个)的相同电子邮件地址并拥有 100 个帐户,但所有密码必须不同,但是只要他们的电子邮件地址不是相同的。

我似乎在任何猫鼬模式文档中都找不到任何此类事情的示例。

使用 adreas 建议我有:

var userSchema = new Schema({
    email: {
        type: String,
        required: true,
        index: true
    },
    password: {
        type: String,
        required: true,
        index: true
    }
});
//either email or password must be unique if the other is not
userSchema.index({
    "email": 1,
    "password": 1
}, {
    unique: true
});

module.exports = mongoose.model('User', userSchema);

它仍然让我使用相同的电子邮件和相同的通行证。

4

1 回答 1

0

复合索引应该是您需要的。

User.index({ email: 1, hashedPassword: 1 }, { unique: true });

但是,像这样实施它可能会带来安全风险,因为您必须在两个帐户上使用相同的盐。只是不要存储纯文本。

于 2013-05-21T15:38:34.227 回答