我尝试使用mongorepository或mongooperations或mongotemplate从mongodb获取今天创建的所有记录。我希望按日期匹配所有记录,而不是在时间戳上。请参考下面我的mongodb enteries。
/* 1 */
{
"_id" : ObjectId("59135f13fc90f22b00c91df2"),
"_class" : "com.vistors.management.domains.EmployeeVisitor",
"checkedIn" : true,
"checkedInDate" : ISODate("2017-05-10T18:42:27.630Z"),
"visitor" : {
"$ref" : "VISITOR",
"$id" : ObjectId("59135f13fc90f22b00c91def")
},
"employee" : {
"$ref" : "EMPLOYEE",
"$id" : ObjectId("59135f13fc90f22b00c91df0")
}
}
/* 2 */
{
"_id" : ObjectId("59135f13fc90f22b00c91df3"),
"_class" : "com.vistors.management.domains.EmployeeVisitor",
"checkedIn" : true,
"checkedInDate" : ISODate("2017-05-10T18:42:27.638Z"),
"visitor" : {
"$ref" : "VISITOR",
"$id" : ObjectId("59135f13fc90f22b00c91dee")
},
"employee" : {
"$ref" : "EMPLOYEE",
"$id" : ObjectId("59135f13fc90f22b00c91df0")
}
}
/* 3 */
{
"_id" : ObjectId("5913ec9eafeb7a1e8df79d5f"),
"_class" : "com.vistors.management.domains.EmployeeVisitor",
"checkedIn" : true,
"checkedInDate" : ISODate("2017-05-11T04:46:22.425Z"),
"visitor" : {
"$ref" : "VISITOR",
"$id" : ObjectId("59135f13fc90f22b00c91dee")
},
"employee" : {
"$ref" : "EMPLOYEE",
"$id" : ObjectId("59135f13fc90f22b00c91df0")
}
}
我想从db中获取与checkInDate匹配的所有enteries作为今天的日期:
@Query("{'checkedInDate': {$gte: ?0, $lte:?0 }}")
List<EmployeeVisitor> findAllCheckedInToday(ZonedDateTime date)
或
@Override
public List<EmployeeVisitor> getTodayRecords() {
//Date date = Date.from(instant);
Query query = new Query().addCriteria(Criteria.where("checkedInDate").is(new Date()));
return mongoTemplate.find(query, EmployeeVisitor.class);
//Criteria.where("expenseDate").gte(calendar1.getTime()).lte(calendar2.getTime()).and("userid").is(uid);}
}
但是我未能达到要求的结果,我无法理解如何从数据库中查询日期而不是带时间戳的日期。
感谢您的帮助。
Jitender
答案 0 :(得分:0)
继续@Veeram的建议。这就是你能做的,
ZonedDateTime today = ZonedDateTime.of(LocalDate.now(), LocalTime.MIDNIGHT, ZoneId.systemDefault());
ZonedDateTime tomorrow = today.plusDays(1);
repository.finByCheckedInDateBetween(Date.from(today.toInstant()), Date.from(tomorrow.toInstant()));
您可以将存储库方法更改为此类
finByCheckedInDateBetween(Date from, Date to)
答案 1 :(得分:0)
您可以将存储库方法更改为以下形式:
List<EmployeeVisitor> findByCheckedInDate(ZonedDateTime date)