将新字段添加到TypeGoose子登记

发布于 2025-02-10 01:53:10 字数 962 浏览 2 评论 0原文

我如何在用户类的子记录中包含一个额外的字段?

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

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

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

  @prop({ ref: () => Car })
  public cars?: Ref<Car>[]; 
}

填充的汽车集合:

Car A
{
  "_id": "1"
  "model": "Ferrari"
}

Car B
{
  "_id": "2"
  "model": "Tesla"
}

根据默认类填充的用户集合:

User
{
  "_id": "1",
  "name": "Jonh Doe",
  "age": 25,
  "cars": [
    { "_id": "1" },
    { "_id": "2" }
  ]
}

我需要在用户集合的汽车阵列中包含一个“状态”字段,如下所示:

  • 状态是确定汽车模型是否为用户使用
User
{
  "_id": "1",
  "name": "Jonh Doe",
  "age": 25,
  "cars": [
    { "_id": "1", "status": false },
    { "_id": "2", "status": true }
  ]
}

How do I include an extra field in the User class subdocument?

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

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

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

  @prop({ ref: () => Car })
  public cars?: Ref<Car>[]; 
}

Populated Car Collection:

Car A
{
  "_id": "1"
  "model": "Ferrari"
}

Car B
{
  "_id": "2"
  "model": "Tesla"
}

User collection populated according to the default class:

User
{
  "_id": "1",
  "name": "Jonh Doe",
  "age": 25,
  "cars": [
    { "_id": "1" },
    { "_id": "2" }
  ]
}

I need to include a "status" field in the cars array of the User collection, as shown below:

  • status is to identify whether the car model is active for the user
User
{
  "_id": "1",
  "name": "Jonh Doe",
  "age": 25,
  "cars": [
    { "_id": "1", "status": false },
    { "_id": "2", "status": true }
  ]
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鯉魚旗 2025-02-17 01:53:10

Hasezoey帮助我解决了GitHub问题。

这是链接和他的回答。

https://github.com/typegoose/typegoose/typegoose/typegoose/discussions/discussions/726

选项:

将当前参考数组作为子数字纪录片(请参阅示例1)
包括汽车本身的状态属性
有一个单独的类,该类在用户卡引用上具有唯一的化合物索引,并且具有您想要的属性,并且在用户中具有这样的参考字段(无论是通过虚拟填充或具有明确的参考阵列)(请参阅示例2)

示例1 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[];
}

示例2:

// the first example does not use virtual populate
class Car {
  @prop()
  public model?: string;
}

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

  @prop({ ref: () => User })
  public user: Ref<User>;

  @prop()
  public extra_properties: string;
}

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

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

  @prop({ ref: () => Car })
  public cars?: Ref<Car>[]; 

  @prop({ ref: () => UserCarProperties })
  public car_properties: Ref<UserCarProperties>[];
}

// the following uses virtual populate
class User {
  @prop()
  public name?: string;

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

  @prop({ ref: () => Car })
  public cars?: Ref<Car>[]; 

  @prop({ ref: () => UserCarProperties, foreignField: 'user', localField: '_id' })
  public car_properties: Ref<UserCarProperties>[]; // this property does not exist in the database, but can still populate
}

poftual

virtual 示例2将汽车的引用合并到UserCarproperties中并使用它来找到汽车,这完全取决于您

个人想要的方式,如果阵列的长度不知道或可能长到未知的长度,则建议使用Virtual Puthulate(合并)示例2

Hasezoey helped me with the github issue.

Here's the link and his answer.

https://github.com/typegoose/typegoose/discussions/726

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[];
}

Example 2:

// the first example does not use virtual populate
class Car {
  @prop()
  public model?: string;
}

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

  @prop({ ref: () => User })
  public user: Ref<User>;

  @prop()
  public extra_properties: string;
}

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

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

  @prop({ ref: () => Car })
  public cars?: Ref<Car>[]; 

  @prop({ ref: () => UserCarProperties })
  public car_properties: Ref<UserCarProperties>[];
}

// the following uses virtual populate
class User {
  @prop()
  public name?: string;

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

  @prop({ ref: () => Car })
  public cars?: Ref<Car>[]; 

  @prop({ ref: () => UserCarProperties, foreignField: 'user', localField: '_id' })
  public car_properties: Ref<UserCarProperties>[]; // this property does not exist in the database, but can still populate
}

Virtual Populate

Alternatively you could also in Example 2 merge the cars references into the UserCarProperties and use that to find cars, that is fully up to how you want it

Personally i would suggest Example 2 with virtual populate (merged) if the array is of unknown length or could grow to unknown length

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文