Search code examples
javamongodbspring-bootspring-data-mongodb

MongoDB truncating LocalDateTime nanosecs when MongoTemplate persists the data


I have a Spring Boot 2 project using MongoDB as database.

When I Instantiate an object containing a date as LocalDateTime by calling LocalDateTime.now(); I receive a datetime containing nanoseconds with more than 4 numerical places.

When I persist this data in mongoDB using mongotemplate.save, the value is saved containing 3 places for nanoseconds only (the rest is filled with zero). So, how can I accomplish the same precision when storing to DB? Or another option: how would I limit the precision when creating a LocalDateTime instance in java?

@Document("Entity")
public class Entity implements Serializable {

    @Id
    private String id;

    @Indexed(name = "created")
    private LocalDateTime created;

    public HistoryEntity(){
        // This gives me 2020-01-30T13:25:09.817182
        this.created = LocalDateTime.now();
    }
}

@Repository
public class DAO {

    @Autowired
    private MongoTemplate mongoTemplate;


    public void save(Entity e) {
        // When value is persisted, I get 2020-01-30T13:25:09.817 if I look into mongoDB instance or if I retrieve the same entity from DB in Java
        Entity saved = mongoTemplate.save(e);
    }
}

Solution

  • I think I found out the answer and probably there isn't much I can do other than truncate my LocalDateTime from java side (As I explained in my last comment and depicted below)

    Clock millisecondClock = Clock.tick(Clock.systemDefaultZone(), Duration.ofNanos(1000000)); 
    LocalDateTime d = LocalDateTime.now(millisecondClock);
    

    I was reading this Baeldung's article https://www.baeldung.com/spring-data-mongodb-zoneddatetime when I faced the following statement (which answered my question):

    ... we're removing the nanoseconds information since the MongoDB Date type has a precision of milliseconds:

    So that's it. Even if I want to work with a higher precision, seems that MongoDB can't handle it (at least for now)