mongodb分页查询、模糊匹配查询、数组元素匹配查询

近期有相关需求,探索完使用方法之后做个记录

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不会实际的存下来)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值