我有两个模型。第一个是 UserSchema,第二个是 CategorySchema
var UserSchema = Schema({
firstName: {
type: String,
required: true
},
secondName: String,
lastName: {
type: String,
required: true
},
email: {
type: String,
unique: true,
required: true
},
password: {
type: String,
required: true
},
status: {
type: String,
required: true
},
roles: [{
type: Schema.ObjectId,
ref: 'Role'
}],
publications: [{
title: {
type: String,
},
description: String,
status: {
type: String,
},
createdAt: {
type: Date
},
updatedAt: {
type: Date,
default: Date.now()
},
pictures: [{
name: String
}],
categories: [{
type: Schema.Types.ObjectId,
ref: 'Category'
}],...
模型类别是
var CategorySchema = Schema({
name: String,
subcategories: [{
name: String
}]
});
UserSchema 有出版物。Publications 包含一个数组。在出版物中是包含子类别 id 数组的类别(子类别在 CategorySchema 的范围内)
问题是当我需要填充 UserSchema 的类别时。UserSchema 的类别具有属于 CategorySchema 的子类别的 _id 数组。