MongoDB assembled on the use of java for advanced query query parameters

    

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

Fuzzy query: or REGEX: orOperator
$ gt: greater than
$ lt: less than
$ gte: greater than or equals
$ lte: less than or equal
Sort: sorting
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;

Guess you like

Origin www.cnblogs.com/mrxiab/p/11684077.html