Spring Data MongoDB Date Between

2020-02-17 06:22发布

I use spring data mongodb.

I want the records between two dates. The following MongoDB Query works:

db.posts.find({startDate: {$gte: start, $lt: end}});

My attempted Spring data query object code translation does not work:

Query query = new Query();
query.addCriteria(Criteria.where("startDate").gte(startDate)
                            .and("startDate").lt(endDate));

What is the correct order of method calls to build the Mongo query I need?

8条回答
欢心
2楼-- · 2020-02-17 07:11

It works, he is some sample code:

Criteria criteria = Criteria.where("pt").gte(startDate)
       .andOperator(Criteria.where("pt").lt(endDate));
查看更多
该账号已被封号
3楼-- · 2020-02-17 07:18

Reference here

Query query = new Query(
  Criteria.where("ip").is(ip)
  .andOperator(
    Criteria.where("createdDate").lt(endDate),
    Criteria.where("createdDate").gte(startDate)
  )
);
查看更多
登录 后发表回答