簡體   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