如何猫鼬深第二虚拟人口?

时间:2020-01-01 00:50:37

标签: javascript node.js mongodb express mongoose

我有SettlementSchema,看起来像这样:

const SettlementSchema = new mongoose.Schema(
    {
        status: { type: String, required: true, enum: OrderStatusEnum },
        applicant: {
            type: Schema.Types.ObjectId,
            required: true,
            refPath: 'applicantModel',
        },
        applicantModel: {
            type: String,
            required: true,
            enum: ['Admin', 'Merchant', 'User'],
        },
    },
    {
        timestamps: true,
        toObject: { virtuals: true },
        toJSON: { virtuals: true },
    },
);

SettlementSchema.virtual('applicant.businesses', {
    ref: 'Business',
    localField: 'applicant._id',
    foreignField: 'merchant',
});

SettlementSchema包含对MerchantSchema的引用,如下所示:

const MerchantSchema = new mongoose.Schema(
    {
        firstName: { type: String, required: true },
        lastName: { type: String, default: null },
        email: { type: String, required: true, unique: true },
        phoneNumber: { type: String, required: true, unique: true },
        password: { type: String, default: null },
    },
    { timestamps: true },
);

和BusinessSchema引用到MerchantSchema

const BusinessSchema = new Schema(
    {
        name: { type: String, required: true },
        slug: {
            type: String,
            slug: 'name',
            unique: true,
            sparse: true,
            slugPaddingSize: 4,
        },
        logo: { type: Schema.Types.ObjectId, ref: 'File' },
        categories: [String],
        merchant: { type: Schema.Types.ObjectId, ref: 'Merchant' },

    },
    {
        timestamps: true,
        toObject: { virtuals: true },
        toJSON: { virtuals: true },
    },
);

这是我的查询:

this.settlement.find()
            .populate({ 
                path: 'applicant', 
                select: '', 
                populate :{ path:"applicant.businesses" }})

因此,使用虚拟填充,我的所有期望将是:

"settlements": [
            {
                "_id": "5e09f6cce220a628345acf5e",
                "applicant": {
                      "_id":"5dcc08593b0ade48c00c471d",
                      "firstName": "Toto",
                      "lastName": "Rubianto",
                      "email": "toto@gmail.com",
                      "phoneNumber": "+6287878786xxx",
                      "password": "xxx",
                      "businneses": [{
                           "_id":"5dcc08593b0ade48c00asd23",
                           "name":"Startbuck",
                           "merchant":"5dcc08593b0ade48c00c471d"
                      },{
                           "_id":"5dcc08593b0ade48c00asd23",
                           "name":"MCD",
                           "merchant":"5dcc08593b0ade48c00c471d"
                      }]
                 },
                "applicantModel": "Merchant",
                "status": "PENDING",
                "createdAt": "2019-12-30T13:08:28.718Z",
                "updatedAt": "2019-12-30T13:08:28.718Z",
                "__v": 0,
            }
        ],

我的问题是申请人。企业无法使用虚拟人群, 我的代码有什么问题?

0 个答案:

没有答案