创建父文档时未初始化子文档。 Nestjs/mongoose

The subdocument is not initialized when the parent document is created. Nestjs/mongoose

创建用户时,子文档未使用默认值初始化。我只得到没有 ecdsa 字段的用户对象。你能告诉我哪里可能犯了错误吗?

P.S。我希望 Ecdsa 成为用户模型的一部分,而不是用户模型拥有此文档的 ID。可能吗?

用户架构:

@Schema()
export class User {
  @Prop({required: true, unique: true })
  tgId: number;

  @Prop({default: "anonymus"})
  tgUsername: string;

  @Prop({default: Date.now()})
  registrationDate: Date;

  @Prop({default: () => ({})})
  ecdsa: Ecdsa;
}

export type UserDocument = User & mongoose.Document;
export const UserSchema = SchemaFactory.createForClass(User);

Ecdsa 架构:

@Schema()
export class Ecdsa {
  @Prop({default: 0}) //doesn't works
  operatorsCount: number;

  @Prop({
    type: [{
      type: mongoose.Schema.Types.ObjectId,
      ref: 'UserOperator'
    }],
    default: []
  })
  operators: UserOperator[];

  @Prop({default: 'test'}) //doesn't works
  test: string;
}

export type EcdsaDocument = Ecdsa & mongoose.Document;
export const EcdsaSchema = SchemaFactory.createForClass(Ecdsa);

不确定这个方法是否正确,但对我有用。现在用户创建时初始化用户内部的 ecdsa 对象。

我刚改成:

@Prop({type: EcdsaSchema,
    default: () => ({})})
  ecdsa: EcdsaDocument;