I am currently working on a project where virtual populates are quite necessary:
There's a customer scheme, a document scheme as well as some others referencing the customer. Inside the customer I want to populate the referenced items.
I already had a working Model using Typegoose, which I had to remove from the project because of late incompatibilities. This is how it worked before:
@prop({
ref: () => DMSDocument,
foreignField: 'linkedCustomers', // compare this value to the local document populate is called on
localField: '_id', // compare this to the foreign document's value defined in "foreignField",
justOne: false
})
public documents: { type: Types.ObjectId, ref: 'DMSDocument' }[];
Now I was trying to achieve this using nestjs/mongoose only after removing typegoose:
@Prop({
virtual: 'documents',
ref: 'DMSDocument',
foreignField: 'linkedCustomers',
localField: '_id',
})
public documents: DMSDocument[];
Virtual getters working just fine since I'm just using
@Schema({ toJSON: { virtuals: true, getters: true }, toObject: { virtuals: true, getters: true }})
The model ist populated like this:
this.customerModel.findOne(params)
.populate('documents', '', 'DMSDocument')
.populate('repairs', '', 'Repair')
I am somehow stuck - I just get empty arrays, no errors. What Am I missing? Are they even possible using nestjs/mongoose?