【发布时间】:2021-07-18 07:15:08
【问题描述】:
我尝试将 ObjectId 中的一些字段仅保存在 mongo db 中,但我发现以下内容无法正常工作:
@ObjectType()
export class User {
@prop()
@Field()
name: string;
@prop({ ref: () => OtherClassA })
@Field()
otherClassA: OtherClassA;
@prop()
@Field()
otherClassB: OtherClassB;
}
@ObjectType()
export class OtherClassB {
@prop()
@Field()
name: string;
@prop({ ref: () => OtherClassB1 })
@Field()
otherClassB1: OtherClassB1;
@prop({ ref: () => OtherClassB2 })
@Field()
otherClassB2: OtherClassB2;
}
我发现存储在 mongo db 中的数据如下:
user {
_id
name
otherClassA: ObjectId("607910cdf9961b0dcf50b5d8")
otherClassB {
otherClassB1 {
// the whole object of otherClassB1
}
otherClassB2 {
// the whole object of otherClassB2
}
}
}
这不是我所期望的......
我预计:
user {
_id
name
otherClassA: ObjectId("607910cdf9961b0dcf50b5d8")
otherClassB {
otherClassB1: ObjectId("607910cdf1231b0dcf51a456")
otherClassB2: ObjectId("607910cdf1231b0dcf51a5bf")
}
}
我如何才能在 typegoose(mongoose) 中仅将引用保存在这种结构中?
2021-4-26 更新:
typegoose v7.6.0
猫鼬 v5.10.18
打字稿 v4.0.5
mongodb v3.6.6
【问题讨论】:
-
请更新版本信息(typegoose、mongoose、typescript、mongodb)
-
@hasezoey 我已经在原始帖子中发布了我的版本。
标签: mongodb typescript mongoose nestjs typegoose