Skip to main content
Relations work a little differently in typegoose when compared to other relational ORMs such as sequelize or typeorm. You can read more about relations (references) in typegoose [here](https://mongoosejs.com/docs/populate .html)
There are multiple ways to set of references in Typegoose. These are intended as a starting point.
Filtering on references is not supported by Typegoose.

One to Many/Many To One Example

To set up a one to many/many to one relationship in Typegoose, you will store a reference in your document. For example, lets add sub tasks to our todo items by storing a todoItem ref on our subTask and an array of sub-tasks on our todoItem entity.
todo-item/todo-item.entity.ts
Now that we have the relationships defined, we can add the @Relation and @Connection to our DTOs
todo-item/todo-item.dto.ts

Many To Many Example

In this example, we’ll add tags to todoItems by storing an array of tag references on the todoItems.
todo-item/todo-item.entity.ts
Now that we have the relationship defined, we can add the @Connection to our DTOS
todo-item/todo-item.dto.ts

Discriminators

Typegoose supports mongoose discriminators. nestjs-query provides support for them through the NestjsQueryTypegooseModule. To use discriminators you need to define them in your NestjsQueryTypegooseModule.forFeature call. When working with discriminators, nestjs-query requires you to provide a service class for each discriminated entity. This is necessary for the auto-generated resolvers to be created correctly. These service classes are also the perfect place to add any custom business logic for your discriminated entities.
todo-item/todo-item.module.ts