我目前正在一个虚拟人口的项目上工作,这是非常必要的:
有一个客户计划、一个文档计划以及其他一些引用客户的计划。在客户计划内我想填充这些引用的内容。
我已经使用 Typegoose 创建了一个可行的模型,在项目中由于后期不兼容性问题而被移除。这就是之前它是如何工作的:
@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' }[];
现在我正在尝试使用 nestjs/mongoose 实现这一点,仅在删除 typegoose 后使用:
@Prop({
virtual: 'documents',
ref: 'DMSDocument',
foreignField: 'linkedCustomers',
localField: '_id',
})
public documents: DMSDocument[];
由于我只是在使用虚拟获取器,所以它能够正常工作。
@Schema({ toJSON: { virtuals: true, getters: true }, toObject: { virtuals: true, getters: true }})
模型的填充方式如下:
this.customerModel.findOne(params)
.populate('documents', '', 'DMSDocument')
.populate('repairs', '', 'Repair')
我卡在这里了 - 我只得到空的数组,没有错误。我错过了什么吗?使用nestjs/mongoose它们是否可能?