【问题标题】:Using Timestamp with nestjs is not updated when update使用带有nestjs的时间戳在更新时不会更新
【发布时间】:2020-12-02 18:47:54
【问题描述】:

我正在将 NestJs 用于后端项目,并尝试使用时间戳来显示更新和创建日期,但没有显示!

    @Schema()
export class Camera extends Document{
 
//   @Prop({required: true, unique: true})
  @Prop({required: true})
  facility_name: string;

  @Prop({required: true, unique : true})
  camera_id: string;
  
  @Prop({required: true})
  camera_location: string;

  @Prop({required: true})
  camera_type: string;

  @Prop({default : false})
  is_deleted : boolean;

  @Prop()
  timestamps: true;

}

export const cameraSchema = SchemaFactory.createForClass(Camera); }

我如何在这个 frmawork 类型中使用时间戳,因为它不显示任何日期!!

【问题讨论】:

    标签: mongodb timestamp nestjs


    【解决方案1】:

    @Schema 装饰器接受模式选项对象作为参数:

    @Schema({
      timestamps: true,
    })
    

    使用此选项,createdAtupdatedAt 属性将被添加到集合的文档中:

    {
      "_id": "5fc3fab191c59905a0931df2",
      "content": "Lorem ipsum dolor sit amet",
      "createdAt": "2020-11-29T19:46:57.199Z",
      "updatedAt": "2020-11-29T19:46:57.199Z",
      "__v": 0
    }
    

    https://mongoosejs.com/docs/guide.html#options查看有关所有选项的扩展信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-24
      • 1970-01-01
      • 2011-02-20
      • 2010-11-05
      • 1970-01-01
      • 1970-01-01
      • 2018-12-26
      相关资源
      最近更新 更多