简体   繁体   English

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

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

I'm trying to fetch all records those have been created on today's date from mongodb using mongorepository or mongooperations or mongotemplate I want to match all records by Date only not on timestamps. 我正在尝试使用mongorepository或mongooperations或mongotemplate从mongodb提取在今天的日期创建的所有记录,我只想按Date匹配所有记录而不是时间戳。 Please refer my mongodb enteries below. 请在下面参考我的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")
}
}

I want to fetch all enteries from db those match with checkedInDate as today's date something like: 我想从数据库中获取所有与checkInDate匹配的条目,作为今天的日期,例如:

@Query("{'checkedInDate': {$gte: ?0, $lte:?0 }}")
List<EmployeeVisitor> findAllCheckedInToday(ZonedDateTime date)

or 要么

@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);}
}

But I'm failed to achieve the required result, I'm not able to understand how can I query from db with date not date with timestamp. 但是我无法达到要求的结果,我无法理解如何从带有日期而不是带有时间戳的日期的数据库中查询。

Thanks for your help. 谢谢你的帮助。

Jitender Jitender

Continuing from what @Veeram suggested. 继续@Veeram的建议。 This is what you can do, 这就是你可以做的

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

And you can change your repository method to something like this 您可以将您的存储库方法更改为如下所示

finByCheckedInDateBetween(Date from, Date to)

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

List<EmployeeVisitor> findByCheckedInDate(ZonedDateTime date)

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

相关问题 Spring Data 的 MongoTemplate 和 MongoRepository 有什么区别? - What's the difference between Spring Data's MongoTemplate and MongoRepository? 如何在春季使用Mongotemplate或MongoRepository从两个集合中获取数据 - How to get data from two collections in spring using Mongotemplate or MongoRepository spring 数据 mongodb ,使用 MongoRepository 还是 MongoTemplate? - spring data mongodb , use MongoRepository or MongoTemplate? MongoRepository JSON日期查询(Spring) - MongoRepository JSON Date Query (Spring) Spring-Data:指定MongoRepository应该使用哪个MongoTemplate - Spring-Data: specify which MongoTemplate a MongoRepository should use Spring 数据 - MongoRepository - @Query 以在嵌套对象列表中查找项目 - Spring Data - MongoRepository - @Query to find item within nested list of objects 如何在使用带有 spring 数据的 MongoRepository 的查询注释时显示查询 - how to show query while using query annotations with MongoRepository with spring data Spring数据mongoRepository查询排序 - Spring data mongoRepository Query sort 使用 Spring 数据 MongoDB 中的 MongoTemplate 进行查找查询时,仅投影某些字段? - Project only certain fields when using MongoTemplate from Spring Data MongoDB for a find query? 将Mongo查询转换为spring Mongooperations - Convert Mongo query to spring Mongooperations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM