【问题标题】:Why spring data mongo not returning the field having time?为什么spring data mongo没有时间返回字段?
【发布时间】:2020-05-21 20:20:51
【问题描述】:

我的收藏中有一个文档,例如

{
        "_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")
} 

我提出的标准是

   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);

但我只获取有时间的字段00,而不是两个文档。我有两份具有该日期的文件,但作为回应,我只得到一份。我尝试了很多事情,但没有成功。我只想比较日期而忽略时间。请帮忙。

【问题讨论】:

    标签: mongodb spring-boot spring-data-mongodb mongotemplate


    【解决方案1】:

    mongod 中的 ISODate 以毫秒分辨率存储为纪元时间戳。

    dateFormat.parse 可能会返回 ISODate("2020-02-05T00:00:00.000Z"),它将作为 1580860800000 存储在数据库中。

    这实际上意味着您的查询是 {scheduledDepDateTime:{$gte: 1580860800000, $lte: 1580860800000}},因此唯一可能满足过滤器的值是 ISODate("2020-02-05T00:00:00.000Z")。

    要获取该日期的所有文档,您可以尝试将您的toDate 设置为第二天,并使用$lt 而不是$lte

    【讨论】:

      【解决方案2】:

      fromto 日期必须分别是该日期的最低时间和最高时间;这将涵盖一天中的所有时间。

      要使用与 $and 运算符相同的字段(“scheduledDepDateTime”),您必须使用CriteriaandOperator 而不是and(请参阅AND Queries With Multiple Expressions Specifying the Same Field)。

      更新后的代码:

      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");
      

      【讨论】:

      • 谢谢@Prasad,但我只得到请求中的日期,如 String from = "2020-02-05" 所以当我执行 DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd H:m :s");它给了我格式异常,因为请求中没有时间。另外,如何在我的 toDate 字段中添加时间,例如 2020-02-05 23:59:59 。因为我的请求中只有日期。
      • 在程序中可以分别附加“00:00:00”和“23:59:59”的请求从/到日期数据(字符串类型)。然后,解析这些字符串以获得所需的fromDatetoDate,您可以将它们用于查询。
      【解决方案3】:

      正如@prasad 所建议的那样todate 必须是该日期的最低时间和最高时间我必须像这样将todate 字段中的时间设置为23:23:59,它可以工作。 p>

       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();
          } 
      

      【讨论】:

        猜你喜欢
        • 2023-04-05
        • 2017-07-07
        • 2019-05-13
        • 1970-01-01
        • 2019-07-23
        • 2020-10-28
        • 1970-01-01
        • 2013-05-20
        • 2016-10-28
        相关资源
        最近更新 更多