繁体   English   中英

使用spring数据通过mongorepository中的@Query或Mongooperations或mongotemplate在今天的日期之前查找

[英]find by today's date using spring data either @Query in mongorepository or Mongooperations or mongotemplate

我正在尝试使用mongorepository或mongooperations或mongotemplate从mongodb提取在今天的日期创建的所有记录,我只想按Date匹配所有记录而不是时间戳。 请在下面参考我的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")
}
}

我想从数据库中获取所有与checkInDate匹配的条目,作为今天的日期,例如:

@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

继续@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)

您可以将存储库方法更改为以下形式:

List<EmployeeVisitor> findByCheckedInDate(ZonedDateTime date)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM