我正在与Mongoose合作开发一个MongoDB数据库. 我有一个Category模型,其中嵌套子类别(SubDocuments)
const schema = new mongoose.Schema(
{
title: {
type: String,
required: true,
},
children: [
{
title: {
type: String,
required: true,
},
},
],
},
{
timestamps: true,
}
)
以及具有引用Category模型的Category字段的产品模型
const schema = new mongoose.Schema(
{
title: {
type: String,
required: true,
},
categories: [{ type: mongoose.Schema.Types.ObjectId, ref: "Category" }],
},
{
timestamps: true,
}
);
类别数组包含同时属于顶级类别和类别的多个_id字段->;子类别
但是当我try 填充产品时.类别:
Product.find()
.populate('categories')
我只获得顶级类别,而不是嵌套子类别.
我如何也填充嵌套级别?
我试过:
Product.find()
.populate({
path: "categories",
model: "Category",
populate: {
path: "children",
model: "Category",
},
})
但它并没有奏效.结果都是一样的