Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. Upon saving ,i see that the dates are being saved in yyyy-MM-dd hh:mm:ss format as shown below.

enter image description here

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.

like image 869
U C Avatar asked Feb 05 '20 23:02

U C


People also ask

How does MongoDB save dates?

MongoDB will store date and time information using UTC internally, but can easily convert to other timezones at time of retrieval as needed.

How does MongoDB store local time?

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.

How does MongoDB store dates in node?

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.

Can we store date as string in MongoDB?

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.

How to store date/time in MongoDB?

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? 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 −

How to use isodate () method 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 (). The syntax is as follows: To understand the above syntax, let us create a collection with documents following the first approach.


1 Answers

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.

like image 85
Joe Avatar answered Nov 14 '22 23:11

Joe