Using Timestamp with nestjs is not updated when update

Viewed 4057

Am using NestJs for a backend project and am trying to use timestamps to show update and create date but nothing shows!

    @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); }

How can I use timestamps with this tpye of frmawork as it doesn't show any date!!

1 Answers

The @Schema decorator accepts as argument a schema options object:

@Schema({
  timestamps: true,
})

With this option the createdAt and updatedAt properties will be added to the documents of the collection:

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

See extended info about all the options at https://mongoosejs.com/docs/guide.html#options

Related