为什么spring data mongo没有时间返回字段? [英] Why spring data mongo not returning the field having time?

查看:45
本文介绍了为什么spring data mongo没有时间返回字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我把标准像

   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 的字段的文档,而不是文档.我有两份该日期的文件,但作为回应,我只收到了一份.我尝试了很多事情,但没有成功.我只想比较日期而忽略时间.请帮忙.

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.

推荐答案

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

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.

要使用与 $and 运算符相同的字段(scheduledDepDateTime"),您必须使用 CriteriaandOperator 而不是 <代码>和(见AND 查询使用多个指定相同字段的表达式).

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

更新后的代码:

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

这篇关于为什么spring data mongo没有时间返回字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆