[英]How to populate sub document of another model in mongoose?
I have two mongodb model as following.我有两个 mongodb model 如下。
const CompanySchema = new Schema(
{
sections: [{
name: { type: String },
budgets: [{ // indicates from CalcSchema
index: { type: Number },
title: { type: String },
values: [Number],
sum: { type: Number, default: 0 },
}],
}]
},
{ timestamps: true }
);
const CalcSchema = new Schema({
budget: {
type: Schema.Types.ObjectId, // I want to populate this field. this indicates budget document in Company model
ref: "Company.sections.budgets" //it's possible in mongoose?
},
expense: {
type: Number,
default: 0
}
});
budget field indicate one of budgets field in CompanySchema.预算字段表示 CompanySchema 中的预算字段之一。 So I want to populate when get Calc data.
所以我想在获取 Calc 数据时进行填充。 But I don't how to populate embedded document.
但我不知道如何填充嵌入式文档。
I tried set ref value to ref: "Company.sections.budgets"
.我尝试将 ref 值设置为
ref: "Company.sections.budgets"
。 but it's not working.但它不起作用。
Please anyone help.请任何人帮助。
Finally, I found answer myself.最后,我自己找到了答案。
There is useful plugin for it.有一个有用的插件。
https://github.com/QuantumGlitch/mongoose-sub-references-populate#readme https://github.com/QuantumGlitch/mongoose-sub-references-populate#readme
And I learned that my schema structure was wrong.我了解到我的模式结构是错误的。 It's anti-pattern in mongodb.
这是 mongodb 中的反模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.