问题
I have two models. The first one is UserSchema and the second one is 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'
}],...
the model category is
var CategorySchema = Schema({
name: String,
subcategories: [{
name: String
}]
});
UserSchema has publications. Publications contains an array. Within of publications is categories that contains an array of id of subcategory(subcategory is whithin of CategorySchema)
the problem is when I need to populate categories of UserSchema. Categories of UserSchema have an array of _id of subcategory that belongs to CategorySchema.
来源:https://stackoverflow.com/questions/50127214/nested-ref-to-populate-mongoose-5-0-nodejs