天天看點

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不會實際的存下來)