Mongoose模式中嵌套字段的唯一索引

时间:2017-02-08 16:26:12

标签: node.js mongoose mean-stack

这些架构是否有效?

var StudentSchema = new mongoose.Schema({
  first_name: {type: String, required:true},
  last_name: {type: String, required: true},
  mother_name : {type: String, required: true},
  siblings:[{
    name:{type: String, required: true},
    age:{type:Number, required: true},
    school:{type:String, required:true}
  }]
});

这个

var WinSchema = new mongoose.Schema({
    event:{type: mongoose.Schema.Types.ObjectId, ref:'Event'},
    winners : [{
        student: {type: mongoose.Schema.Types.ObjectId, ref: 'Student'},
        position: {type:String, enum:["First","Second","Third","Consolation"]}
  }]
});

WinSchema.index({event:1,winners.student:1},{unique:true});

mongoose.model('Win',WinSchema);

我们可以如StudentSchema中所示嵌套吗? 我们可以在嵌套文档上创建唯一索引吗?

1 个答案:

答案 0 :(得分:0)

是的,您可以嵌套它,请参阅架构示例here,当然您可以在嵌套文档上创建索引example