I want to store an email string and password where only 1 needs to be unique. So someone can use the same email address with 2 different passwords (or 100) and have 100 accounts but all the passwords must be different however someone should still be able to have the same password as someone else as long as their email address is not the same.
I can't seem to find any example of this sort of thing in any of the mongoose schema docs.
using adreas suggestion I have:
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);
It's still letting me use the same email with the same pass.
Compound index should be what you need.
User.index({ email: 1, hashedPassword: 1 }, { unique: true });
Implementing it like this might be a security risk though, as you would have to have the same salt on both accounts. Just don't store plain-text pls.