1

I am using spring data with mongodb. The problem is that the entity contains a date field which we receive data for from a MS SQL database in UTC timezone by default. Example date is below.

2017-12-06 14:50:17.630

When we store the entity into mongodb it actually substracts 5:30 hrs from the date and then stores it in ISO format for example above date would be stored like

"submittedDate" : ISODate("2017-12-06T09:20:17.630Z"),

My entity is something like below:

public class LegalEntity {
    private String name;
    private Date updateDate;

}

To me it seems Mongo is by default considering that the dates are coming in IST and hence it converts these to UTC and then stores and returns the same in UTC as well.

Is there anyway to tell mongo that the date is coming in UTC by default and don't convert it?

Vinay Prajapati
  • 7,199
  • 9
  • 45
  • 86
  • What do you get if you query mongo, do you get 2017-12-06T09:20:17.630 or 2017-12-06 14:50:17.630 as the returned date? – pvpkiran Feb 23 '18 at 10:37
  • I get `2017-12-06T09:20:17.630` . – Vinay Prajapati Feb 23 '18 at 10:40
  • How is date field mapped from the front end ? Your date is already interpreted in local time zone when it is mapped from front end. – s7vr Feb 23 '18 at 10:46
  • there is no front-end buddy. Mongo converts the passed in Date to UTC considering it is passed in IST format and return the UTC date when fetched. So, I want to tell mongo to persist the passed in date as is as its already in UTC – Vinay Prajapati Feb 23 '18 at 10:48
  • Okay. Let me ask you this. How do you create that date object ? – s7vr Feb 23 '18 at 10:51
  • Actually JPA is doing this for me as I am using MS SQL database to get the data which then gets stored to Mongo. So no explicit code to do this. – Vinay Prajapati Feb 23 '18 at 10:54
  • 1
    I don't think you can avoid this. Other option is to store date as String and use custom converter. https://stackoverflow.com/questions/34791481/spring-mongodb-timestamp-timezone-misleading – pvpkiran Feb 23 '18 at 11:02

0 Answers0