猫鼬的多重一对多关系

时间:2016-06-06 22:08:58

标签: node.js mongodb mongoose

我正在使用mongoose存储三种模式的文档,有时候我必须更新之间的引用,为此我使用的是mongoose-relationship plugin, 我需要的是这样的参考:

  • 一位客户有很多时间表,
  • 一位顾客有很多订单,
  • 一个订单有很多时间表

当我创建订单时,我需要按时间表id来按顺序引用。但我每个集合只能引用一个childPath,我的模型就像这样映射;

客户:

var CustomerSchema = new Schema({
    name: {type: String, required: true},
    email: {type: String, required: true},
    shedules: [{ type:mongoose.Schema.Types.ObjectId, ref:"Schedule" }],
    orders: [{ type:mongoose.Schema.Types.ObjectId, ref:"Order" }]
}

时刻表:

var ScheduleSchema = new Schema({
    customer: {type:mongoose.Schema.Types.ObjectId, ref:"Customer", childPath:"shedules"}, //shedule id
    order: {type:mongoose.Schema.Types.ObjectId, ref:"Order", childPath:"shedules"}, //order Id
    sequence: {type: Number, default: 0},
    creation_date: {type: Date, default: Date.now}
}
SheduleSchema.plugin(relationship, {relationshipPathName:['customer','order']});

订单:

var OrderSchema = new Schema({
    customer: {type:mongoose.Schema.Types.ObjectId, ref:"Customer", childPath:"order"},
    shedules: [{type:mongoose.Schema.Types.ObjectId, ref:"Shedule" }],// <-- this field doesn't update.
    price: {type: Number, default: 0}
}
OrderSchema.plugin(relationship, { relationshipPathName:'customer' });

0 个答案:

没有答案