1.1、按条件查询列表和统计
//统计数量
public Integer count(Bean domin) {
Query query = getCondition(domin);
return (int)template.count(query, Bean.class, Bean.COLLECTION);
}
//查找列表
public List<Bean> getBeanList(Bean domin) {
Query query = getCondition(domin);
query = query.with(Sort.by(Sort.Direction.DESC, "updateDate"))
.skip((domin.getPageNo() - 1) * domin.getPageSize())
.limit(domin.getPageSize());
List<Bean> beans = template.find(query, Bean.class, Bean.COLLECTION);
if (CollectionUtils.isEmpty(beans)) {
return Collections.emptyList();
}
beans.stream().forEach(v -> {
//处理列表信息
buildBean(v);
});
return beans;
}
/** 编辑各种查询条件 */
private Query getCondition(Bean domin) {
Query query = new Query();
//普通查询
if (StringUtils.isNotBlank(domin.getCusId())) {
query.addCriteria(Criteria.where("cusId").is(domin.getCusId()));
}
//模糊查询
if (StringUtils.isNotBlank(domin.getCusName())) {
String str = "^.*"+domin.getCusName()+".*$";
Pattern pattern= Pattern.compile(str, Pattern.CASE_INSENSITIVE);
query.addCriteria(Criteria.where("cusName").regex(pattern));
}
//in 判断元素是否在指定的集合范围里
if (StringUtils.isNotBlank(domin.getPayProgress())) {
query.addCriteria(Criteria.where("payProgress").in(1,2,3));
}
//时间判断查询
if (domin.getBeginTime() != null && domin.getEndTime() != null) {
query.addCriteria(Criteria.where("updateDate").gte(new Date(domin.getBeginTime()))
.lte(new Date(domin.getEndTime())));
}
if (domin.getBeginTime() != null && domin.getEndTime() == null) {
query.addCriteria(Criteria.where("updateDate").gte(new Date(domin.getBeginTime())));
}
if (domin.getBeginTime() == null && domin.getEndTime() != null) {
query.addCriteria(Criteria.where("updateDate").lte(new Date(domin.getEndTime())));
}
return query;
}
1.2、根据条件查找单个对象
Query query = Query.query(Criteria.where("userName").is(bean.getUserName())
.and("curriculum").is(bean.getCurriculum())
.addCriteria(Criteria.where("payProgress").lt(5))
.with(Sort.by(Sort.Direction.DESC, "updateDate")).skip(0).limit(1);
Bean bean = template.findOne(query, Bean.class, Bean.COLLECTION);
1.3、根据条件 Distinct 去重
Query query = new Query();
if(!CollectionUtils.isEmpty(advisers)){
query = new Query(Criteria.where("code").in(codes));
}
List<String> result = template.findDistinct(query,"curriculum",Bean.COLLECTION,String.class);
1.4、or查询
Criteria criteria = new Criteria();
criteria.orOperator(Criteria.where("cusId").is('123'),Criteria.where("payProgress").is(3));
Query query = Query.query(criteria);
List<Bean> beans = template.find(query, Bean.class, Bean.COLLECTION);
1.5、查询指定字段或排除某个字段
Query query = new Query();
query.addCriteria(Criteria.where("id").is('1234'));
query.fields().include("cusName");//包含该字段
query.fields().exclude("cusId");//不包含该字段
query.fields().include("updateDate");
query.addCriteria(Criteria.where("updateDate").gte(beginTime).lte(endTime));
List<Bean> list = mongoTemplate.find(query, Bean.class, Bean.COLLECTION);
返回值为新增后的对象
Bean bean = new Bean();
bean.setCusId("1245");
bean.setUpdateDate(new Date());
Bean rs = template.insert(bean, Bean.COLLECTION);
Query query = Query.query(Criteria.where("cusId").is("123"));
template.remove(query,EduPaymentProgressBean.COLLECTION);
Query query = Query.query(Criteria.where("cusId").is(1234).and("payProgress").lt(5));
Update update = Update.update("updateDate", new Date()).set("payProgress", 3);
//updateFirst 更新查询会返回结果集的第一条
template.updateFirst(query, update, Bean.COLLECTION);
//修改多条符合条件的
template.updateMulti(query, update, Bean.class);
Bean对象:
@Data
@Document(collection = Bean.COLLECTION)
public class Bean implements Serializable {
public static final String COLLECTION = "mongoDb_table_name";
private String id;
private String cusId;
private String cusName;
private Integer payProgress;
private Date updateDate;
// 页码
private Integer pageNo = 1;
// 每页数量
private Integer pageSize = 10;
private Long beginTime;
private Long endTime;
}