mongodb查询条件设定


//查询条件
Query query = new Query();
//查询条件
Criteria criteria = new Criteria();
SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );

if (StringUtils.isNotEmpty(type) && !type.equals("-1")) {
criteria.and("type").is(type);
}

String contacter=entrustInfoVo.getContacter();
if (StringUtils.isNotEmpty(contacter)) {
criteria.and("jsonObj.contacter").is(contacter);
}

String provinceId=entrustInfoVo.getProvinceId();
String provinceName=entrustInfoVo.getProvinceName();
List<Criteria> list = new ArrayList<Criteria>();
if (StringUtils.isNotEmpty(provinceId) || StringUtils.isNotEmpty(provinceName)) {
criteria.and("signupRecordHandleVO.provinceId").is(provinceId);
list.add(Criteria.where("signupRecordHandleVO.provinceName").is(provinceName));
}
//卖房委托和租房委托的数据不显示(1:卖房委托 2:租房委托)
list.add(Criteria.where("type").in("3","4","5","6","7","8","9","10","11","12","13","14"));
//处理状态
String handleFlag=entrustInfoVo.getHandleFlag();
if (StringUtils.isNotEmpty(handleFlag)) {
criteria.and("signupRecordHandleVO.handleFlag").is(Integer.parseInt(handleFlag));
}

if (list.size() > 0) {
criterial.orOperator(list.toArray(new Criteria[list.size()]));
criteria.andOperator(criterial);
}
query.addCriteria(criteria);
//总条数
long count = mongoTemplate.count(query, EntrustJsonObj.class, TableConstants.STEWARD_ENTRUST_INFO);
System.out.print(query.toString());
//分页
query.skip((pageNo - 1) * pageSize);// skip相当于从哪条记录开始
query.limit(pageSize);// 从skip开始,取多少条记录

//排序
Sort sort = new Sort(Sort.Direction.DESC, "createTime");
query.with(sort);

pageBean.setTotalCount(count);
posted @ 2016-12-28 16:15  lcpnene  阅读(1629)  评论(0编辑  收藏  举报