如何在nestjs mongodb中加载嵌入的参考模式?
目前我正在使用 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;
}
其中以用户作为参考架构。现在我必须在查询购买模式时使用填充来获取用户。像这样。
buyModel.find().populate('user');
我可以在不使用 populate 的情况下自动加载吗?
Currently I am working on API using nestjs. It has the code like below.
@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;
}
which has user as reference schema. Right now I have to use populate to get user while querying Buy schema. like this.
buyModel.find().populate('user');
Could I automatically load without using populate?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 mongoose-autopopulate 插件,或
post
钩子,如 mongoose 官方文档中所述。You can use mongoose-autopopulate plugin, or the
post
hook, as is described in the mongoose official documentation.