MongoDB Java 查询

分页查询等

 @RequestMapping("getWsMonitorInfo")
    public String getWsMonitorInfo(MonitorInfo monitorInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
        String type = request.getParameter("type");
        Criteria criteria = new Criteria();
        if(StringUtils.isBlank(monitorInfo.getReqSyscode())){
            monitorInfo.setReqSyscode(null);
        }
        if(StringUtils.isBlank(monitorInfo.getServiceName())){
            monitorInfo.setServiceName(null);
        }
        Page monitorInfoPage = null;
        Query query = new Query();

        if (type != null) {

            switch (type) {
                case "startPoint":
//                    criteria=    Criteria.where("reqSyscode").is(null).and("serviceName").is(null);
                    monitorInfo.setReqSyscode(null);
                    monitorInfo.setServiceName(null);
                    break;
                case "callSystem":
                    criteria=   Criteria.where("reqSyscode").is(monitorInfo.getReqSyscode()).and("serviceName").is(monitorInfo.getServiceName());
                    break;
                case "totalCall":
                    criteria=  Criteria.where("reqSyscode").is(monitorInfo.getReqSyscode()).and("serviceName").is(monitorInfo.getServiceName());
                    break;
                case "successCall":
                    criteria=    Criteria.where("reqSyscode").is(monitorInfo.getReqSyscode()).and("serviceName").is(monitorInfo.getServiceName()).and("ifSuccess").is("1");
                    monitorInfo.setIfSuccess("1");
                    break;
                case "failCall":
                    criteria=    Criteria.where("reqSyscode").is(monitorInfo.getReqSyscode()).and("serviceName").is(monitorInfo.getServiceName()).and("ifSuccess").is("0");
                    monitorInfo.setIfSuccess("0");
                    break;
                case "proxyService":
                    criteria=    Criteria.where("serviceName").is(monitorInfo.getServiceName());
                    monitorInfo.setReqSyscode(null);
                    break;
            }
            monitorInfoPage = new Page<>(0, 10);
        } else {
            criteria= null;
            if(StringUtils.isNotBlank(monitorInfo.getReqSyscode())){
                query.addCriteria( Criteria.where("reqSyscode").is(monitorInfo.getReqSyscode()));
            }
            if(StringUtils.isNotBlank(monitorInfo.getServiceName())){
                query.addCriteria( Criteria.where("serviceName").is(monitorInfo.getServiceName()));
            }
            if(StringUtils.isNotBlank(monitorInfo.getIfSuccess())){
                query.addCriteria( Criteria.where("ifSuccess").is(monitorInfo.getIfSuccess()));            }
            }
            monitorInfoPage = new Page<>(request, response);

        Sort sort = new Sort(Sort.Direction.DESC,"createDate");
        if(criteria!=null){
            query.addCriteria(criteria);
        }
        Long total = mongoOperations.count(query,MonitorInfo.class);
        query.skip(((monitorInfoPage.getPageNo()-1)>0?monitorInfoPage.getPageNo()-1:0)*monitorInfoPage.getPageSize()).limit(monitorInfoPage.getPageSize()).with(sort);
        List monitorInfos = mongoOperations.find( query,MonitorInfo.class);
        monitorInfoPage.setCount(total);
        monitorInfoPage.setList(monitorInfos);
//        Page page = monitorInfoService.findPage(monitorInfoPage, monitorInfo);
        model.addAttribute("page", monitorInfoPage);
        return "bi/monitor/monitorInfoList";

    }

 

注意点: mongon中 字段存在和字段为null不等价。空串和null也不等价。

字段不存在 Criteria.where("ifSuccess").not();且不能与其他条件用and或者or拼接

字段为空 Criteria.where("ifSuccess").is(null);

字段为空串 Criteria.where("ifSuccess").is("");

建索引

db.monitorInfo.createIndex(
    {
        reqSystemCode: 1,
        serviceName: 1,
        createDate: 1
    },
    {
        name: "reqSystemCode_1_serviceName_1_createDate_1"
    }
)

 

更新db.monitorInfo.update({}, 
    {$set:{}}, 
    { multi: false, upsert: false}
)

multi:是否全部更新,upsert是否插入更新(找不到一样的就插入);

查询不存在某个字段的数据

db.getCollection(‘test’).find({res_name:{$exists:false}})

你可能感兴趣的:(MongoDB Java 查询)