I'm trying to pull a ref from a Subdocument. The Documents looks like this.
Sample Categories Doc:
{
"_id": 12345
"keyword": "smart phones",
"subCategories": [
{
"_id": 6789,
"keyword": "apple"
},
{
"_id": 101123,
"keyword": "samsung"
}
]
}
Sample Dictionary Doc:
{
"_id": 12345
"keyword": "iPhone",
"category": 12345,
"subCategory": 6789
}
Here's what I've tried to do on Typegoose model definition:
For Dictionary (notice the subCategory prop, I'm not sure if that's the right way to reference a subdocument):
export class Dictionary extends Typegoose {
_id!: Schema.Types.ObjectId;
@prop({
default: 1
})
type!: IDictionaryInput['type'];
@prop()
mainKeyword!: string;
@arrayProp({ items: Synonym })
synonyms!: Synonym[];
@prop({ ref: Category })
category!: Ref<Category>;
@prop({ ref: CategoryModel.subCategories })
subCategory!: Ref<Subcategory>;
@prop({
default: true
})
status!: IDictionaryInput['status'];
}
For Categories:
export class Category extends Typegoose {
_id!: Schema.Types.ObjectId;
@prop()
keyword!: ICategoryInput['keyword'];
@prop({
default: 1
})
type!: ICategoryInput['type'];
@arrayProp({ items: Subcategory })
subCategories!: Subcategory[];
@prop({
default: true
})
status!: ICategoryInput['status'];
@prop()
insertTimestamp!: ICategoryInput['insertTimestamp'];
}
Then I tried to populate the references by doing:
DictionaryModel.findOne({ _id: id })
.populate({
path: 'category',
model: CategoryModel
})
.populate({
path: 'subCategory',
model: CategoryModel.subCategories
});
I can successfully populate the ref from category but not on subCategory.