MongoRepository findByCreatedAtBetween 没有返回准确的结果 [英] MongoRepository findByCreatedAtBetween not returning accurate results

查看:82
本文介绍了MongoRepository findByCreatedAtBetween 没有返回准确的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Mongo 中的文档结构是这样的:

My document structure in Mongo is like this :

db.user.find()

db.user.find()

{
        "_id" : ObjectId("560fa46930a8e74be720009a"),
        "createdAt" : ISODate("2015-10-03T09:47:56.333Z"),
        "message" : "welcome",
}
{
            "_id" : ObjectId("560fa46930a8e723e720009a"),
            "createdAt" : ISODate("2015-10-03T09:48:25.048Z"),
            "message" : "thank you"
}

当我在 Mongo Shell 中使用以下查询在两个给定时间戳之间查找文档时,我得到了正确的结果:

When I use the below query in my Mongo Shell to find documents between two given timestamps, i get the right result :

db.user.find({createdAt:{$gte:ISODate("2015-10-03T09:40:25.048Z"),$lte:ISODate("2015-10-03T09:50:56.333Z")}})

我在我的 REST 服务中使用 MongoRepository 和 Spring 来处理 Java 驱动程序.以下是用户存储库:

I am using MongoRepository with Spring in my REST service to work with the Java driver. Below is the user repository:

public interface UserRepository extends MongoRepository<User, String> 
{
       ArrayList<User> findbyCreatedAtBetween(Date d1, Date d2);

}

当我在我的服务中调用 make 以下调用时,它不返回任何结果userRepository.findbyCreatedAtBetween(2015-10-03T09:40:25.048Z, 2015-10-03T09:50:29.006Z)

When I call make the following call in my service, it does not return any result userRepository.findbyCreatedAtBetween(2015-10-03T09:40:25.048Z, 2015-10-03T09:50:29.006Z)

但是,当我将 d1 作为前一天给出时,它会返回结果:userRepository.findbyCreatedAtBetween(2015-10-02T09:40:25.048Z, 2015-10-03T09:50:29.006Z)

However it returns the result when I give d1 as previous day : userRepository.findbyCreatedAtBetween(2015-10-02T09:40:25.048Z, 2015-10-03T09:50:29.006Z)

有关如何解决此问题的任何想法?请帮忙!

Any ideas on how I can resolve this? Please help!

推荐答案

细分下来,正在对 MongoDB 数据库执行带有关键字 Between 的查询,逻辑结果为 {"createdAt" : {"$gt" : d1, "$lt" : d2}} 所以你有可能没有得到包含 createdAt 日期的文档给定的日期范围即 d1 因为给定的日期范围不满足条件.作为参考,这些是对 查询方法:

Breaking it down, the query with the keyword Between is being executed against the MongoDB database with the logical result {"createdAt" : {"$gt" : d1, "$lt" : d2}} so there is a chance that you are not getting the documents that have the createdAt date inclusive within the given date range i.e. d1 < createdAt < d2 since the given date range does not satisfy the criteria. For reference these are some of the interpretations on the query methods:

查询方法支持的关键字

Keyword     Sample                              Logical result
After       findByBirthdateAfter(Date date)     {"birthdate" : {"$gt" : date}}
Before      findByBirthdateBefore(Date date)    {"birthdate" : {"$lt" : date}}
Between     findByAgeBetween(int from, int to)  {"age" : {"$gt" : from, "$lt" : to}}

作为一种解决方法,您可能需要使用 @Query 注释.我尚未对此进行测试,但您可能想尝试以下自定义查询实现示例:

As a workaround, you may want to use the @Query annotation. I haven't tested this but you may want to try the following custom query implementation example:

public interface UserRepository extends MongoRepository<User, String>  {
    @Query(value = "{ 'createdAt' : {$gte : ?0, $lte: ?1 }}")
    public ArrayList<User> findbyCreatedAtBetween(Date from, Date to);
}

如果以上方法对您不起作用,请创建一个自定义接口和您的实现类来执行自定义查询.例如,创建一个接口,其名称附加 Custom:

If the above doesn't work for you, create a custom interface and your implementation class to execute the custom query. For example, create an interface with a name that appends Custom:

public interface UserRepositoryCustom {
    public List<User> findbyCreatedAtBetween(Date from, Date to); 
}

修改UserRepository,增加要扩展的UserRepositoryCustom接口:

Modify the UserRepository and add the UserRepositoryCustom interface to be extended:

@Repository
public interface UserRepository extends UserRepositoryCustom, MongoRepository {

}

创建您的实现类来实现UserRepositoryCustom 接口中定义的方法.

Create your implementation class to implement the methods defined in UserRepositoryCustom interface.

public class UserRepositoryImpl implements UserRepositoryCustom {

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public ArrayList<User> findbyCreatedAtBetween(Date from, Date to) {
        return mongoTemplate.find(
            Query.addCriteria(Criteria.where("createdAt").gte(from).lte(to));
    }
}

这篇关于MongoRepository findByCreatedAtBetween 没有返回准确的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆