In my user document, I want to individually index email
and username
as unique fields, so that a duplicate is never entered.
The problem is all of the documentation I have found for multiple unique indexes is for "Compound Indexing", which appears to somehow tie the second index to the first. I don't want that.
All I want is for in my signup step 1 if a user submits an email that already exists for MongoDB to return an error that it already exists, and exact same for step 2, when they set their username.
So I want them indexed and unique separate from each other. I'm using this now and it is tieing the 2 together somehow which is not what I want:
const UserSchema = new mongoose.Schema({
email: {
type: String,
required: true,
trim: true
},
username: {
type: String,
required: false,
trim: true
}
})
UserSchema.index({
email: 1,
username: 1
}, {
unique: true
});