如何在猫鼬中填充另一个模型的子文档?
How to populate sub document of another model in mongoose?
我有两个 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
}
});
预算字段表示 CompanySchema 中的预算字段之一。
所以我想在获取 Calc 数据时填充。
但是我不知道如何填充嵌入式文档。
我尝试将 ref 值设置为 ref: "Company.sections.budgets"
。但它不起作用。
请大家帮忙。
终于,我自己找到了答案。
有一个有用的插件。
https://github.com/QuantumGlitch/mongoose-sub-references-populate#readme
而且我了解到我的模式结构是错误的。它是 mongodb.
中的反模式
我有两个 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
}
});
预算字段表示 CompanySchema 中的预算字段之一。 所以我想在获取 Calc 数据时填充。 但是我不知道如何填充嵌入式文档。
我尝试将 ref 值设置为 ref: "Company.sections.budgets"
。但它不起作用。
请大家帮忙。
终于,我自己找到了答案。
有一个有用的插件。
https://github.com/QuantumGlitch/mongoose-sub-references-populate#readme
而且我了解到我的模式结构是错误的。它是 mongodb.
中的反模式