如何确保对象在猫鼬模式中没有相同的两个元素?
How to make sure that objects do not have the same two element in mongoose schema?
我正在制作以下猫鼬模式,我想确保没有对象具有相同的 autherFirstName 和 autherLastName。对象可能有一个共同点,但不会同时有两个
const authorShcema = new mongoose.Schema({
autherFirstName: {type: String, minLength: 2, required: true},
autherLastName: {type: String, minLength: 2, required: true},
autjorDob: {type: Date, required: true},
authorImage: {type: String},
authorBooks: [{type: mongoose.Schema.Types.ObjectId, ref: "Book"}],
});
https://mongoosejs.com/docs/2.7.x/docs/indexes.html
创建复合唯一索引
authorShcema.index({ autherFirstName: 1, autherLastName: 1 }, { unique: true });
我正在制作以下猫鼬模式,我想确保没有对象具有相同的 autherFirstName 和 autherLastName。对象可能有一个共同点,但不会同时有两个
const authorShcema = new mongoose.Schema({
autherFirstName: {type: String, minLength: 2, required: true},
autherLastName: {type: String, minLength: 2, required: true},
autjorDob: {type: Date, required: true},
authorImage: {type: String},
authorBooks: [{type: mongoose.Schema.Types.ObjectId, ref: "Book"}],
});
https://mongoosejs.com/docs/2.7.x/docs/indexes.html
创建复合唯一索引
authorShcema.index({ autherFirstName: 1, autherLastName: 1 }, { unique: true });