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. Upon saving ,i see that the dates are being saved in yyyy-MM-dd hh:mm:ss format as shown below.
Can someone let me know why is this happening? Is there a way to just save the LocalDate in yyyy-MM-dd format? Thank you so much.
Im using SpringJPA Mongo with springboot and Java.
MongoDB will store date and time information using UTC internally, but can easily convert to other timezones at time of retrieval as needed.
MongoDB stores times in UTC by default, and will convert any local time representations into this form. Applications that must operate or report on some unmodified local time value may store the time zone alongside the UTC timestamp, and compute the original local time in their application logic.
The best format to store date and time in MongoDB is native javascript Date() or ISO date format as it internally converts it into BSON native Date object.
MongoDB has a Date BSON type that allows you to store dates as dates. You can also store dates as strings, if that's what you need.
There are two different ways by which you can store date/time in MongoDB. In the first approach, you can use Date objects like JavaScript. The Date object is the best way to store date/time in MongoDB. The syntax is as follows: In the second approach, you can use ISODate ().
How to use save () correctly in MongoDB? How to use save () correctly in MongoDB? Use the db.collection.save () to update an existing document or inserts a new document, depending on its document parameter. Let us create a collection with documents −
In the first approach, you can use Date objects like JavaScript. The Date object is the best way to store date/time in MongoDB. The syntax is as follows: In the second approach, you can use ISODate (). The syntax is as follows: To understand the above syntax, let us create a collection with documents following the first approach.
This is happening because MongoDB stores data in BSON format (see the BSON spec).
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. UTC datetime is the number of milliseconds since epoch, leading to the time portion being all zeroes when it is rendered.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With