有什么办法可以完成下面的施工吗?目前,我可以通过直接在子模式中直接定义必要的字段来完成我需要的操作。但是,由于相同的信息在数组内的所有子文档中重复,我想知道是否可以将此信息汇总到顶层。
如果我简单地定义一个嵌套模式,它无法使用
ValidationError: records.0.d: Path "d" is required.
计算默认值
const childSchema = new Schema({
c: Number,
d: {
type: Number,
required: true,
default: function () {
// the part I need help with
return this.parent.a + this.parent.b + this.c;
}
}
});
const parentSchema = new Schema({
a: Number,
b: Number,
records: [childSchema]
});
parent()
代替,如下所示:
const childSchema = new Schema({
c: Number,
d: {
type: Number,
required: true,
default: function () {
return this.parent().a + this.parent().b + this.c;
}
}
});