近期有相关需求,探索完使用方法之后做个记录
Query query = new Query();
if(StringUtils.isNotEmpty(name)){
//Pattern pattern = Pattern.compile("^张$", Pattern.CASE_INSENSITIVE); //完全匹配
//Pattern pattern = Pattern.compile("^.*张$", Pattern.CASE_INSENSITIVE); //右匹配
//Pattern pattern = Pattern.compile("^张.*$", Pattern.CASE_INSENSITIVE); //左匹配
Pattern pattern = Pattern.compile("^.*"+ name +".*$", Pattern.CASE_INSENSITIVE); //模糊匹配
query.addCriteria(Criteria.where("title").regex(pattern));
}
if(StringUtils.isNotEmpty(userName)){
query.addCriteria(Criteria.where("userName").is(userName));
}
if(StringUtils.isNotEmpty(airline)){
query.addCriteria(Criteria.where("airline").is(airline));
}
Long count = mongoTemplate.count(query, ReplyTemplate.class);
//分页
Pageable pageable = new PageRequest(pageNumber - 1, pageSize);
query.with(pageable);
//提交时间排序
query.with(new Sort(Sort.Direction.DESC, "updateTime"));
List<ReplyTemplate> feedbackList = mongoTemplate.find(query, ReplyTemplate.class);
PagedResult<ReplyTemplate> result = new PagedResult<>(count, (int)(count/pageSize + 1), pageNumber, pageSize, feedbackList);
总结几点:
1:分页使用Pageable(大家有什么好的方法欢迎留言)
2:query.with(new Sort(Sort.Direction.DESC, "updateTime"));或者
Sort sort = Sort.by(new Sort.Order(Sort.Direction.DESC, "replyDate").nullsLast()); query.with(sort); //需要处理空值的
3:模糊匹配用正则
Pattern pattern = Pattern.compile("^.*"+ name +".*$", Pattern.CASE_INSENSITIVE);
query.addCriteria(Criteria.where("title").regex(pattern));
4:如果查询条件是某个类型是数组的key中包含某个值,则使用:
condition.put("NLP_RESULT.2", new BasicDBObject("$elemMatch", new BasicDBObject("$eq", keyWord)));
其中NLP_RESULT.2代表NLP_RESULT类型为数组并且第三个元素作为key,$elemMatch是匹配数组中包含某个元素的关键字。
具体的使用如下(mongo查询的另一种组装方法,指定查询条件并且指定对应的projection)
BasicDBObject condition = new BasicDBObject();
condition.put("FLT_DATE", new BasicDBObject("$gte", startDate).append("$lte", endDate));
condition.put("AIRLINE", airline);
condition.put("CMNT", new BasicDBObject("$ne", null));
if(!StringUtils.isEmpty(keyWord)){
condition.put("NLP_RESULT.2", new BasicDBObject("$elemMatch", new BasicDBObject("$eq", keyWord)));
}
if(!StringUtils.isEmpty(commentType)){
condition.put("NLP_RESULT.1", commentType);
}
if(!StringUtils.isEmpty(fltType)){
condition.put("FLT_TYPE", fltType);
}
if(isReply != null){
if(isReply){
condition.put("replyDate", new BasicDBObject("$exists", 1));
}else{
condition.put("replyDate", null);
}
}
BasicDBObject field = new BasicDBObject();
field.put("CMNT", 1);
field.put("replyDate", 1);
field.put("userName", 1);
field.put("R_OVAL", 1);
field.put("NLP_RESULT", 1);
Query query = new BasicQuery(condition.toJson(), field.toJson());
Long count = mongoTemplate.count(query, CommentNLP.class);
//分页
Pageable pageable = new PageRequest(pageNumber - 1, pageSize);
query.with(pageable);
//提交时间排序
Sort sort = Sort.by(new Sort.Order(Sort.Direction.DESC, "replyDate").nullsLast());
query.with(sort);
List<CommentNLP> commentNLPList = mongoTemplate.find(query, CommentNLP.class);
PagedResult<CommentNLP> result = new PagedResult<CommentNLP>(count, (int)(count/pageSize + 1) ,pageNumber, pageSize, commentNLPList);
5:condition.put("replyDate", new BasicDBObject("$exists", 1)); //存在该字段
condition.put("replyDate", null); //不存在该字段或者该字段为空(查阅相关资料,mongo把null不会实际的存下来)