SpringBoot+Mongodb多条件模糊查询(超详细,有效)

不想扣字了,直接上代码:

      public Msg<Page<WindowsSysLog>> windowsSysLogQuery(@RequestParam(value = "page",required = false) Integer page,@RequestParam(value = "pageNum",required = false) Integer pageNum,@RequestParam(value = "clientId",required = false) String clientId, @RequestParam(value = "syslogLevel",required = false) String syslogLevel,@RequestParam(value = "startDate",required = false) Long startDate,@RequestParam(value = "endDate",required = false) Long endDate,@RequestParam(value = "msg",required = false) String msg) {
        Msg msg1 = Msg.ok();
        Criteria c1=null;
        Criteria c2=null;
        Criteria c3=null;
        ArrayList<Criteria> list1=new ArrayList<Criteria>();
        if (clientId != null){
            c1 = Criteria.where("clientId").regex(".*?" + clientId + ".*?");
            list1.add(c1);
        }
        if (syslogLevel != null){
            c2 = Criteria.where("syslogLevel").regex(".*?" +syslogLevel + ".*?");
            list1.add(c2);
        }
        if (msg != null){
            c3 = Criteria.where("msg").regex(".*?" + msg + ".*?");
            list1.add(c3);
        }
        Criteria[] arr=new Criteria[list1.size()];
        list1.toArray(arr);
        Criteria criteria=new Criteria().andOperator(arr);
        Query query=new Query(criteria);
        if (startDate !=null && endDate !=null){
            query.addCriteria(Criteria.where("createDate").gte(startDate).lte(endDate));
        }
        if (list1.size()==0){
            query=new Query();
        }

分页懒得发了,网上有,可以私聊

你可能感兴趣的:(SpringBoot,Mongo)