关于mongoDB使用java实现高级查询query参数的组装

    

import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

模糊查询:regex or:orOperator
$gt:大于
$lt:小于
$gte:大于或等于
$lte:小于或等于
Sort:排序
Query query = new Query();
query.addCriteria(Criteria.where("orgId").is(queryShopActionLogReqVO.getOrgId()));
query.addCriteria(Criteria.where("logType").is(queryShopActionLogReqVO.getLogType()));
Criteria criteriaDay = null;
if(!StringUtils.isEmpty(queryShopActionLogReqVO.getStartTime())) {
criteriaDay = Criteria.where("loginTime").gte(queryShopActionLogReqVO.getStartTime());
}
if(!StringUtils.isEmpty(queryShopActionLogReqVO.getEndTime())) {
if(criteriaDay == null){
criteriaDay = Criteria.where("loginTime").lte(queryShopActionLogReqVO.getEndTime());
}else{
criteriaDay.lte(queryShopActionLogReqVO.getEndTime());
}
}
if(criteriaDay != null){
query.addCriteria(criteriaDay);
}
if(!StringUtils.isEmpty(queryShopActionLogReqVO.getKeyWord())) {
query.addCriteria(new Criteria ().orOperator(Criteria.where("userCode").regex(queryShopActionLogReqVO.getKeyWord())
,Criteria.where("userName").regex(queryShopActionLogReqVO.getKeyWord())
,Criteria.where("account").regex(queryShopActionLogReqVO.getKeyWord())));
}

query.with(new Sort(Sort.Direction.DESC, "loginTime"));
return query;
上一篇:java-具有Spring Data Pageable的JPA Criteria查询总和


下一篇:Mybatis中Example的用法