nested ref to populate mongoose 5.0 nodejs

戏子无情 提交于 2020-02-06 08:24:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!