如何在nestjsmongodb中加载嵌入的引用模式



目前我正在使用nestjs开发API。它的代码如下。

@ObjectType()
@Schema()
@InputType('BuyInput', { isAbstract: true })
export class Buy extends CoreEntity {
@Prop(raw([BuyItemType]))
@Field(() => [BuyItemType])
items: BuyItemType[];
@Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'User' })
user: User;
@Prop({
type: String,
enum: BuyStatus,
})
@Field(() => BuyStatus)
@IsEnum(BuyStatus)
status: BuyStatus;
@Field()
@Prop({ name: 'deliveryCharge' })
deliveryCharge: number;
@Field()
@Prop({ name: 'finalCharge' })
finalCharge: number;
}

其具有用户作为参考模式。现在,在查询Buy模式时,我必须使用populate来获取用户。像这样。

buyModel.find().populate('user');

我可以在不使用populate的情况下自动加载吗?

您可以使用mongoose自动填充插件或post钩子,如mongoose官方文档中所述。

最新更新