【发布时间】:2021-04-24 12:45:01
【问题描述】:
我有两个 mongodb 模型如下。
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 字段表示 CompanySchema 中的预算字段之一。 所以我想在获取 Calc 数据时进行填充。 但我不知道如何填充嵌入文档。
我尝试将参考值设置为ref: "Company.sections.budgets"。但它不起作用。
请任何人帮忙。
【问题讨论】:
-
请阅读此链接,我知道它对您有用并解决其他填充问题:dev.to/paras594/how-to-use-populate-in-mongoose-node-js-mo0
-
这能回答你的问题吗? How to populate nested entities in mongoose?
-
@mohammadjavadahmadi 感谢您的评论。但这对我的情况没有帮助。我想填充其他模式的子文档。您的答案的情况是从一个模式中的子文档填充其他模式。情况不同。
标签: node.js mongodb mongoose mongoose-populate