When attempting to save certain fields in ObjectId only in the mongo db, I encountered an issue with the code below:
@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;
}
After saving the data, it appeared as shown below in the mongo db:
user {
_id
name
otherClassA: ObjectId("607910cdf9961b0dcf50b5d8")
otherClassB {
otherClassB1 {
// the whole object of otherClassB1
}
otherClassB2 {
// the whole object of otherClassB2
}
}
}
This result was not expected....
The desired outcome was:
user {
_id
name
otherClassA: ObjectId("607910cdf9961b0dcf50b5d8")
otherClassB {
otherClassB1: ObjectId("607910cdf1231b0dcf51a456")
otherClassB2: ObjectId("607910cdf1231b0dcf51a5bf")
}
}
How can I ensure that only references are saved in this structure using typegoose(mongoose)?
2021-4-26 Updated:
typegoose v7.6.0
mongoose v5.10.18
typescript v4.0.5
mongodb v3.6.6