使用 Java 查询 MongoDb 日期范围
Querying MongoDb for a date Range using Java
我的 mongoDb 集合中有一些数据和一个字段名称 billingDate,我在其中以 ISO 格式存储账单日期,如下所示。
{
"_id":"xxyy",
"name":"abcd",
"billingDate":ISODate("2018-01-03T13:50:05.000+0000"),
}
我想查询我的集合的日期范围,例如获取从一个日期到另一个日期的所有账单。为此,我从用户那里获取日期,然后将其转换为 mongoDb 的匹配格式。
fromTime // value from user in format("dd/mm/yyyy")
toTime // value from user in format("dd/mm/yyyy")
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy");
dtf = dtf.withLocale(Locale.US); // Locale specifies human language for translating, and cultural norms for lowercase/uppercase and abbreviations and such. Example: Locale.US or Locale.CANADA_FRENCH
LocalDate fdate = LocalDate.parse(fromTime, dtf);
LocalDate tdate = LocalDate.parse(toTime, dtf);
System.out.println("date is :: "+fdate);
LocalDateTime flocalDateTime=LocalDateTime.of(fdate, LocalTime.MIDNIGHT);
Instant finstant=flocalDateTime.toInstant(ZoneOffset.UTC);
LocalDateTime tlocalDateTime=LocalDateTime.of(tdate, LocalTime.MIDNIGHT);
Instant tinstant=tlocalDateTime.toInstant(ZoneOffset.UTC);
System.out.println("from TIme is :: "+finstant);
System.out.println("to time is :: "+tinstant);
现在当我为 $gte 查询 mongodb 数据时它工作正常但是当我使用 $lte它给我错误。
Date fdate=Date.from(finstant);
Date tdate=Date.from(tinstant);
Query query= new Query();
query.addCriteria(Criteria.where("queryBillDate").gte(fdate));
query.addCriteria(Criteria.where("queryBillDate").lte(tdate));
List<OPDBill> listOfBills = mongoTemplate.find(query, OPDBill.class,OPDBillsCollection);
错误日志如下:
SEVERE: Servlet.service() for servlet [spring-dispatcher] in context with path [/ClipCare1.0] threw exception [Request processing failed; nested exception is org.springframework.data.mongodb.InvalidMongoDbApiUsageException: Due to limitations of the com.mongodb.BasicDBObject, you can't add a second 'queryBillDate' criteria. Query already contains '{ "queryBillDate" : { "$gte" : { "$date" : "2018-01-01T00:00:00.000Z"}}}'.] with root cause
org.springframework.data.mongodb.InvalidMongoDbApiUsageException: Due to limitations of the com.mongodb.BasicDBObject, you can't add a second 'queryBillDate' criteria. Query already contains '{ "queryBillDate" : { "$gte" : { "$date" : "2018-01-01T00:00:00.000Z"}}}'.
at org.springframework.data.mongodb.core.query.Query.addCriteria(Query.java:99)
at com.aventyn.hms.dao.BillingDAOImpl.findDocRevBill(BillingDAOImpl.java:369)
at com.aventyn.hms.controller.LoginController.getDocRevenue(LoginController.java:180)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
我已经更新了我的问题,并将我的实例更改为最新实例,但现在它又给我一个错误。
我看到你在使用 Spring。尝试使用 MongoOperations:
Date fdate=Date.from(finstant);
Date tdate=Date.from(tinstant);
List<OPDBill> listOfBills = mongoOperations.find(
Query.query(Criteria.where("queryBillDate").gte(fDate).lt(tDate)),
OPDBill.class,
OPDBillsCollection);
我的 mongoDb 集合中有一些数据和一个字段名称 billingDate,我在其中以 ISO 格式存储账单日期,如下所示。
{
"_id":"xxyy",
"name":"abcd",
"billingDate":ISODate("2018-01-03T13:50:05.000+0000"),
}
我想查询我的集合的日期范围,例如获取从一个日期到另一个日期的所有账单。为此,我从用户那里获取日期,然后将其转换为 mongoDb 的匹配格式。
fromTime // value from user in format("dd/mm/yyyy")
toTime // value from user in format("dd/mm/yyyy")
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd/MM/yyyy");
dtf = dtf.withLocale(Locale.US); // Locale specifies human language for translating, and cultural norms for lowercase/uppercase and abbreviations and such. Example: Locale.US or Locale.CANADA_FRENCH
LocalDate fdate = LocalDate.parse(fromTime, dtf);
LocalDate tdate = LocalDate.parse(toTime, dtf);
System.out.println("date is :: "+fdate);
LocalDateTime flocalDateTime=LocalDateTime.of(fdate, LocalTime.MIDNIGHT);
Instant finstant=flocalDateTime.toInstant(ZoneOffset.UTC);
LocalDateTime tlocalDateTime=LocalDateTime.of(tdate, LocalTime.MIDNIGHT);
Instant tinstant=tlocalDateTime.toInstant(ZoneOffset.UTC);
System.out.println("from TIme is :: "+finstant);
System.out.println("to time is :: "+tinstant);
现在当我为 $gte 查询 mongodb 数据时它工作正常但是当我使用 $lte它给我错误。
Date fdate=Date.from(finstant);
Date tdate=Date.from(tinstant);
Query query= new Query();
query.addCriteria(Criteria.where("queryBillDate").gte(fdate));
query.addCriteria(Criteria.where("queryBillDate").lte(tdate));
List<OPDBill> listOfBills = mongoTemplate.find(query, OPDBill.class,OPDBillsCollection);
错误日志如下:
SEVERE: Servlet.service() for servlet [spring-dispatcher] in context with path [/ClipCare1.0] threw exception [Request processing failed; nested exception is org.springframework.data.mongodb.InvalidMongoDbApiUsageException: Due to limitations of the com.mongodb.BasicDBObject, you can't add a second 'queryBillDate' criteria. Query already contains '{ "queryBillDate" : { "$gte" : { "$date" : "2018-01-01T00:00:00.000Z"}}}'.] with root cause
org.springframework.data.mongodb.InvalidMongoDbApiUsageException: Due to limitations of the com.mongodb.BasicDBObject, you can't add a second 'queryBillDate' criteria. Query already contains '{ "queryBillDate" : { "$gte" : { "$date" : "2018-01-01T00:00:00.000Z"}}}'.
at org.springframework.data.mongodb.core.query.Query.addCriteria(Query.java:99)
at com.aventyn.hms.dao.BillingDAOImpl.findDocRevBill(BillingDAOImpl.java:369)
at com.aventyn.hms.controller.LoginController.getDocRevenue(LoginController.java:180)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
我已经更新了我的问题,并将我的实例更改为最新实例,但现在它又给我一个错误。
我看到你在使用 Spring。尝试使用 MongoOperations:
Date fdate=Date.from(finstant);
Date tdate=Date.from(tinstant);
List<OPDBill> listOfBills = mongoOperations.find(
Query.query(Criteria.where("queryBillDate").gte(fDate).lt(tDate)),
OPDBill.class,
OPDBillsCollection);