【问题标题】:How to make sure that objects do not have the same two element in mongoose schema?如何确保对象在猫鼬模式中没有相同的两个元素?
【发布时间】:2021-03-18 15:43:16
【问题描述】:

我正在制作以下猫鼬模式,并且我想确保没有对象具有相同的 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"}],     
});

【问题讨论】:

    标签: javascript node.js mongodb mongoose mongoose-schema


    【解决方案1】:

    https://mongoosejs.com/docs/2.7.x/docs/indexes.html

    创建复合唯一索引

    authorShcema.index({ autherFirstName: 1, autherLastName: 1 }, { unique: true });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-07
      • 2020-03-10
      • 1970-01-01
      • 2017-11-28
      • 2015-12-18
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      相关资源
      最近更新 更多