嵌套引用填充mongoose 5.0 nodejs

时间:2018-05-02 04:34:14

标签: node.js mongoose mongoose-schema mongoose-populate mongoose-plugins

我有两个型号。第一个是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有出版物。出版物包含一个数组。在出版物中是包含子类别的id数组的类别(子类别是CategorySchema的whithin)

问题是当我需要填充UserSchema的类别时。 UserSchema的类别具有属于CategorySchema的子类别的_id数组。

0 个答案:

没有答案