Why spring data mongo not returning the field having time?

筅森魡賤 提交于 2021-02-17 05:45:28

问题


I have a document in my collection like

{
        "_id" : ObjectId("5e3aaa7cdadc161d9c3e8014"),
        "carrierType" : "AIR",
        "carrierCode" : "TK",
        "flightNo" : "2134",
        "depLocationCode" : "DEL",
        "arrLocationCode" : "LHR",
        "depCountryCode" : "DELHI",
        "arrCountryCode" : "LONDON",
        "scheduledDepDateTime" : ISODate("2020-02-05T00:30:00Z")

}
{
        "_id" : ObjectId("5e3aaacddadc161d9c3e8015"),
        "carrierType" : "AIR",
        "carrierCode" : "TK",
        "flightNo" : "2021",
        "depLocationCode" : "DEL",
        "arrLocationCode" : "LHR",
        "depCountryCode" : "DELHI",
        "arrCountryCode" : "LONDON",
        "scheduledDepDateTime" : ISODate("2020-02-05T00:00:00Z")
} 

I am putting criteria like

   Criteria criteria = new Criteria();
    criteria = criteria.and("carrierCode").is("TK");
     String from = "2020-02-05";
      String to = "2020-02-05";
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    Date toDate = dateFormat.parse(to);
                    Date fromDate = dateFormat.parse(from);
                    criteria = criteria.and("scheduledDepDateTime").gte(fromDate).lte(toDate);

But i am getting document only the field which have time 00 not both the document. I have two documents with that date but in response getting only one. I have tried so many things but not succeed. I want to compare only the date and ignore the time. Please help.


回答1:


The from and to dates must be the lowest time and the highest time for that date, respectively; this will cover all the hours of the day.

For using the same field ("scheduledDepDateTime") with the $and operator you must use the Criteria's andOperator not the and (see AND Queries With Multiple Expressions Specifying the Same Field).

The updated code:

Criteria criteria = new Criteria();
criteria = criteria.and("carrierCode").is("TK");

String from = "2020-02-05 00:00:00";
String to = "2020-02-05 23:59:59";
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd H:m:s");
Date toDate = dateFormat.parse(to);
Date fromDate = dateFormat.parse(from);

criteria = criteria.andOperator(where("scheduledDepDateTime").gte(fromDate), where("scheduledDepDateTime").lte(toDate)));

// Query qry = new Query(criteria);
// List<SomeClassName> result = mongoTemplate.find(qry, SomeClassName.class, "collection_name");



回答2:


The ISODate in mongod is stored as an epoch timestamp with millisecond resolution.

dateFormat.parse is likely returning ISODate("2020-02-05T00:00:00.000Z"), which would be stored in the db as 1580860800000.

This effectively means your query is {scheduledDepDateTime:{$gte: 1580860800000, $lte: 1580860800000}}, so the only possible value that could satisfy the filter is ISODate("2020-02-05T00:00:00.000Z").

To get all documents with that date, you might try making your toDate be the following day, and use $lt instead of $lte.




回答3:


As suggested by @prasad todate must be the lowest time and the highest time for that date I have to set time in todate field to 23:23:59 like this and it works.

 public static Date convertToDate(final Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    } 


来源:https://stackoverflow.com/questions/60083930/why-spring-data-mongo-not-returning-the-field-having-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!