After registering my Schema with mongoose using Dynamic ref, I followed the documentation available at: https://mongoosejs.com/docs/populate.html#dynamic-ref
@Schema({ collection: 'quotations' })
export class QuotationEntity {
@Prop({
required: true,
enum: {
values: ['PersonalClientEntity', 'CommercialClientEntity'],
message: 'Please supply a valid client type. Allowed: \'PersonalClientEntity\' or \'CommercialClientEntity\'.'
},
type: String
})
clientType: String;
@Prop({ type: MongooseSchema.Types.ObjectId, refPath: 'ClientType', required: true })
clientRef: Types.ObjectId;
}
I save an ObjectId under the clientRef
field which should reference the clientType
field. When I use the populate()
method, it needs to populate either 'PersonalClientEntity' or 'CommercialClientEntity'.
When running the query:
await this._model.find({ companyRef: companyId }).populate('clientRef').exec();
No population occurs. However, replacing the ref
in my Schema and providing the correct reference manually like so:
@Prop({ type: MongooseSchema.Types.ObjectId, ref: 'PersonalClientEntity', required: true })
clientRef: Types.ObjectId;
The populate()
method works as expected. Am I missing something in my Schema with the refPath
, or is there something else I am overlooking?