Monday, 18 November 2019

springboot mongo, auto add createdDate and lastModifyDate on your table (collection)

You actually don't need manually add timestamp and update them programtically for your mongo collections.

You can just add below 4 lines into your POJO:

private Instant lastModifiedDate;

private Instant createdDate;

and then add below line to your application:


the database will manage it for you.

however if you give ID when you are creating a new document, then the database will consider you are updating, so you will never get "createDate"

as above link mentioned, you have 2 workaround, i suggest you use @version. you just need to add below to your POJO:


private String version;

Next, you need to make sure you copy them over when you update them otherwise you may lost them.

No comments:

Post a comment