Lets say we have this model :
const userSchema = new mongoose.Schema({
username: {
type: String,
unique: true,
uniqueCaseInsensitive: true,
required: true,
},
organisation: {
type: Schema.Types.ObjectId,
ref: "organisation",
}
});
const UserModel: mongoose.model("user", userSchema),
and we already have this document in our collection :
{
_id: "62e10be79f7ef6f083ca7827",
username : "John",
organisation : {
"$oid": "62d08a16bf95e75ccbf23dcc"
}
}
and we try to store a document with the same username
we will get E11000 duplicate key error collection
What if i want this unique validation to be triggered only when organisation
is the same as well ?
So it will NOT ALLOW this to be stored :
const user = new UserModel({
name: "John",
organisation: "62d08a16bf95e75ccbf23dcc",
});
await user.save();
But it will ALLOW this :
const user = new UserModel({
name: "John",
organisation: "32d08y78bf95i75ccbf56dca",
});
await user.save();
2
Answers
You can define compound index on schema level:
collation option relaxes strength to 2 to make it case-insensitive
use mongoose-unique-validator package.