【发布时间】:2021-10-11 09:14:18
【问题描述】:
我正在尝试使用带有嵌套路径但带有引用路径的 Mongoose populate() 功能:
子架构:
let child = new Schema({
item: {
type: Schema.Types.ObjectId,
required: true,
refPath: "itemType",
},
itemType: {
type: String,
required: true,
enum: ["typeA", "typeB"],
},
});
父架构:
let parent = new Schema({
children: [child],
});
构建模型后,我运行查询:
let mQuery1 = Parent.find().populate({
/* Type 1, does not populate at all. */ path: "children",
populate: { path: "item" },
});
这什么都不做。
let mQuery2 = Parent.find().populate({
path: "children",
populate: [
{ path: "item", model: "typeA" },
{ path: "item", model: "typeB" },
],
});
此查询最终会填充一种类型的字段,而其他类型则不填充(例如,填充 typeA 并为 typeB(s) 返回 null)。
所以我的问题是,如何填充使用 refPath 的嵌套字段?
干杯。
【问题讨论】:
标签: node.js mongodb mongoose mongoose-populate