简体   繁体   English

日期范围存储在数据库中

[英]Date range storing in database

I am trying to store the daterange value in database using mongodb so I used String return type but I am not able to save the daterange . 我试图使用mongodb将daterange值存储在数据库中,所以我使用了String返回类型,但是我无法保存daterange。 Is it posiible to save the daterange directly in the database or I have to split the daterange as start date and end date and store in the database? 将日期范围直接保存在数据库中是否可行,或者我必须将日期范围拆分为开始日期和结束日期并存储在数据库中? Frontend is AngularJS and backend is mongoDB... 前端是AngularJS,后端是mongoDB ...

MongoDB does not provide any kind of DateRange data type. MongoDB不提供任何类型的DateRange数据类型。

Java8 provides Period and Duration types. Java8提供了PeriodDuration类型。 Before Java8 there's the great Joda Time library that also provides Period and Duration data types. 在Java8之前,有一个很棒的Joda Time库,它还提供了Period和Duration数据类型。

The MongoDB library for Java, does not have any sort of converter for those types. Java的MongoDB库没有针对这些类型的任何转换器。 It only knows java.util.Data . 它只知道java.util.Data

Though, if you want to store a Java8 or Joda Period or Duration , you need to either write a customer data type converter for the Java MongoDB Driver, or you do the conversion directly in your application code. 但是,如果要存储Java8或Joda PeriodDuration ,则需要为Java MongoDB Driver编写客户数据类型转换器,或者直接在应用程序代码中进行转换。 (Both would be pretty much the same). (两者几乎相同)。

This way you can save a date range as { range: {from: ISODate("2015-01-01T00:00:00.000Z"), to: ISODate("2015-01-02T00:00:00.000Z") } } in MongoDB. 这样,您可以将日期范围另存为{ range: {from: ISODate("2015-01-01T00:00:00.000Z"), to: ISODate("2015-01-02T00:00:00.000Z") } }在MongoDB中。

Then of course you have to do your queries on the range.from and range.to fields. 然后,您当然必须对range.fromrange.to字段进行查询。


The second part of course is, that the value from the HTML Date Range Picker get's serialized properly to String and can be read by your Java backend. 当然,第二部分是将HTML日期范围选择器中的值正确序列化为String,并且可以由Java后端读取。

Though make sure (eg by simply using a System.out.println(...) ) that the data was correctly deserialized by Java. 虽然要确保(例如,仅通过使用System.out.println(...) ),但Java正确反序列化了数据。

If that all works you can deal with MongoDB, ie converting a DateRange object to two java.util.Date objects, etc. pp. 如果一切正常,则可以使用MongoDB,即将DateRange对象转换为两个java.util.Date对象,等等。pp。

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

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