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<MonitorInfo> 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<MonitorInfo> monitorInfos = mongoOperations.find( query,MonitorInfo.class);
        monitorInfoPage.setCount(total);
        monitorInfoPage.setList(monitorInfos);
//        Page<MonitorInfo> 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}})

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章