I would like to have a combination of 3 keys (type, parent and name) as unique compound index. I defined the schema as follows, but I can still create a unit that has the same type, parent and name. What am I missing?
const unitSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
type: {
type: String,
required: true,
},
parent: {
type: String,
required: true,
},
administrators: {
type: Array,
required: true,
},
});
unitSchema.index({ type: 1, parent: 1, name: 1 }, { unique: true });
const Unit = mongoose.model('Unit', unitSchema);