简体   繁体   English

如何在没有时间的情况下将 LocalDate 保存到 MongoDB(为什么即使我只保存日期,mongo 也会随时间保存日期)?

[英]How to save LocalDate to MongoDB without time (why is mongo saving date with time even if i save just the date)?

Im trying to save List< LocalDate > in mongo (just the date without time in yyyy-MM-dd format), After my API completes the calculation of datesList , im trying to save those dates in MongoDB.我试图在 mongo 中保存List< LocalDate > (只是yyyy-MM-dd格式的没有时间的日期),在我的 API 完成了 dateList 的计算后,我试图将这些日期保存在 MongoDB 中。 Upon saving ,i see that the dates are being saved in yyyy-MM-dd hh:mm:ss format as shown below.保存后,我看到日期以yyyy-MM-dd hh:mm:ss格式保存,如下所示。

在此处输入图片说明

Can someone let me know why is this happening?有人可以让我知道为什么会这样吗? Is there a way to just save the LocalDate in yyyy-MM-dd format?有没有办法只以 yyyy-MM-dd 格式保存 LocalDate? Thank you so much.非常感谢。

Im using SpringJPA Mongo with springboot and Java.我将 SpringJPA Mongo 与 springboot 和 Java 一起使用。

This is happening because MongoDB stores data in BSON format (see the BSON spec ).这是因为 MongoDB 以 BSON 格式存储数据(请参阅BSON 规范)。

There is no datatype in BSON for Date , there is only Timestamp and UTC Datetime both of which are stored as a 64-bit integer. BSON 中没有Date数据类型,只有TimestampUTC Datetime两者都存储为 64 位整数。 UTC datetime is the number of milliseconds since epoch, leading to the time portion being all zeroes when it is rendered. UTC 日期时间是自纪元以来的毫秒数,导致时间部分在呈现时全部为零。

If you want to store just the date, you'll need to use a string type.如果只想存储日期,则需要使用字符串类型。

If the issue is how the data is displayed, you'll just need a different function to convert the timestamp returned from MongoDB to the display format you want to use.如果问题是数据的显示方式,您只需要一个不同的函数来将从 MongoDB 返回的时间戳转换为您想要使用的显示格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM