I am making the following mongoose schema and i want to make sure that no object has the same autherFirstName and autherLastName. object may have one in common but not both of them
JavaScript
x
8
1
const authorShcema = new mongoose.Schema({
2
autherFirstName: {type: String, minLength: 2, required: true},
3
autherLastName: {type: String, minLength: 2, required: true},
4
autjorDob: {type: Date, required: true},
5
authorImage: {type: String},
6
authorBooks: [{type: mongoose.Schema.Types.ObjectId, ref: "Book"}],
7
});
8
Advertisement
Answer
https://mongoosejs.com/docs/2.7.x/docs/indexes.html
Create a composite unique index
JavaScript
1
2
1
authorShcema.index({ autherFirstName: 1, autherLastName: 1 }, { unique: true });
2