Skip to content

Add a new field to the Typegoose subdocument #726

Answered by hasezoey
willmartinsmg asked this question in Q&A
Discussion options

You must be logged in to vote

You have multiple options:

  • make the current reference array a subdocumentarray (see example 1)
  • include the status property on the car itself
  • have a separate class that has a unique compound index on the user-car references and has the properties you want and have such a reference field in the user (either through virtual populate or with a explicit ref array) (see example 2)

Example 1:

class Car {
  @prop()
  public model?: string;
}

class UserCarSub {
  @prop({ ref: () => Car })
  public car: Ref<Car>;

  @prop()
  public extra_properties: string;
}

class User {
  @prop()
  public name?: string;

  @prop({ required: true })
  public age!: number; 

  @prop()
  public cars?: UserCarSub[]

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@willmartinsmg
Comment options

Answer selected by willmartinsmg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #725 on June 25, 2022 12:34.